python - How do I convert upgrouped Data to Grouped Data in Pandas -
so have dataset customers in store , sales of store on each day.
which looks -
store id sales customers 1 250 500 2 276 786 3 124 256 5 164 925
how convert grouped data, this
sales customers 0-100 0 100-200 1181 200-300 1286
i have searched while , found pandas site - http://pandas.pydata.org/pandas-docs/version/0.15.2/groupby.html
df2.groupby(['x'], sort=true).sum()
but unable understand how apply same example.
use cut
bins , groupby
, aggregate sum
:
df = df.groupby(pd.cut(df['sales'], [0,100,200,300]))['customers'].sum().fillna(0) print (df) sales (0, 100] 0.0 (100, 200] 1181.0 (200, 300] 1286.0 name: customers, dtype: float64
also possible define labels:
l =['0-100','100-200','200-300'] b = [0,100,200,300] df = df.groupby(pd.cut(df['sales'], bins=b, labels=l))['customers'].sum() .fillna(0) .reset_index() print (df) sales customers 0 0-100 0.0 1 100-200 1181.0 2 200-300 1286.0
Comments
Post a Comment