2017-04-16 111 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