Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decile python pandas dataframe by column value, and then sum each decile?

Say a dataframe only has one numeric column, order it desc.

What I want to get is a new dataframe with 10 rows, row 1 is sum of smallest 10% values then row 10 is sum of largest 10% values.

I can calculate this via a non-pythonic way but I guess there must be a fashion and pythonic way to achieve this.

Any help?

Thanks!

like image 407
Windtalker Avatar asked Jul 21 '17 18:07

Windtalker


1 Answers

You can do this with pd.qcut:

df = pd.DataFrame({'A':np.random.randn(100)})

# pd.qcut(df.A, 10) will bin into deciles
# you can group by these deciles and take the sums in one step like so:
df.groupby(pd.qcut(df.A, 10))['A'].sum()
# A
# (-2.662, -1.209]   -16.436286
# (-1.209, -0.866]   -10.348697
# (-0.866, -0.612]    -7.133950
# (-0.612, -0.323]    -4.847695
# (-0.323, -0.129]    -2.187459
# (-0.129, 0.0699]    -0.678615
# (0.0699, 0.368]      2.007176
# (0.368, 0.795]       5.457153
# (0.795, 1.386]      11.551413
# (1.386, 3.664]      20.575449

pandas.qcut documentation

like image 142
cmaher Avatar answered Oct 18 '22 19:10

cmaher