Python Pandas Group by Column A and Sum Contents of Column B -


i have python panda data frame like:

       b 0  aa    4 1  bb    6 3  aa    12 4  bb    2 

i want group column , sum values of column b. using following code:

df.groupby(by=['a'])['b'].sum() 

what is:

         b         0  aa    16 1  bb    14 

but not want, want get:

       b 0  aa    16 1  bb    14 

so can access column df['a'] or column b df['b'].

how can this? or suggestion appreciated.

you can use as_index=false option:

in [34]: df.groupby('a', as_index=false)['b'].sum() out[34]:       b 0  aa  16 1  bb   8 

by default, pandas set column use group index. can reset_index afterwards.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -