程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Record the grouping statistics function of pandas, agg

編輯:Python

Mainly to record pandas Study , I want to make a Statistics recently , I look back to the past stata There is a very useful function , Namely tabstat, Statistics can be grouped , And output many indicators .

Recently, my data is like this .

I want to follow valid_num grouping , Statistics avg_stand The average of , Standard deviation , Maximum , Minimum . The first thought is stata Of tabstat, It used to be very comfortable . Now change python 了 . It feels like it can be used groupby, But I don't know how to use . Baidu once .

group1 = df_avg.groupby('valid_num')
group1['avg_stand'].agg(['mean', 'std', 'count'])

It can be used in this way , Output some , Basically what you want .

You can get this result .

in addition , I need to put valid_num In a group , Like sharing ,

valid_grp = pd.cut(valid_num, [0, 1, 2, 3, 4, 5, 10, 50, 100, 200])

After grouping , In Statistics , It's OK, too .

df_avg['vld_grp'] = valid_grp
group2 = df_avg.groupby('vld_grp')
group2['avg_stand'].agg(['mean', 'std', 'max', 'min', 'median']).to_clipboard()

The result is :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved