Pandas Multiindex Series Processing

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Pandas Multiindex Series Processing



I have a pandas 2 index serie that I got from a .groupby() on my original DataFrame:


label ncsc
False 0 297
1 537
2 333
3 207
4 51
5 12
6 4
7 2
True 0 29
1 68
2 35
3 29
4 35
5 18
6 8
7 2
Name: ncsc, dtype: int64



I would like to be able to compute for each 'ncsc' the True rate, ie, for 'ncsc'=6, True rate = 8/(4+8) = 0.66.
Do you see a way to do that with =out using a loop but with a 'pandas synthax'??



Thanks




1 Answer
1



Convert the wanted index level to a column and group by that column:


df.reset_index(level=1).groupby('ncsc').mean()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived