2017-04-16 139 views
-1

数据集:骨料熊猫柱

enter image description here

需要象下面输出​​在使用数据帧大熊猫。我想根据PRCP范围按PRCP进行分组并统计计数。请指教

enter image description here

+1

使用'cut'功能PRCP分为箱和组由箱:http://pandas.pydata.org/pandas -docs /稳定/生成/ pandas.cut.html – foglerit

回答

0
import pandas as pd 

df = pd.DataFrame({'CLDATE':['1/1/16','1/10/16','1/11/16','11/12/16','11/13/16','11/14/16','11/15/16','11/16/16'], 
        'count':[64396,49877,41603,41124,45839,45846,52719,59626],'PRCP':[0,1.8,0,0,0,0,0,0.24]}) 


df['precipate_Range']=pd.cut(df['PRCP'],[0,1,2,3],right=False,labels=['0-1','1-2','2-3']) 

df.groupby('precipate_Range')['count'].agg({'Sum':'sum'}).reset_index() 

输出:

precipate_Range  Sum 
0    0-1 351153.0 
1    1-2 49877.0 
2    2-3  NaN