Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas how to get the mean value of a column base on the index

I have a df you can have it by running this code:

import pandas as pd
from io import StringIO

df = """
 Model      Final
f0901        4
f0901        2
rf0902       2
rf0902       5
rf0902       3
indi0902     4
indi0902     3
indi0902     2
indi0902     1

"""
df= pd.read_csv(StringIO(df.strip()), sep='\s+')
df.set_index("Model", inplace = True)

The output is:

         Final
Model   
f0901       4
f0901       2
rf0902      2
rf0902      5
rf0902      3
indi0902    4
indi0902    3
indi0902    2
indi0902    1

Now how can I get the mean value of ‘Final’ for each ‘Model’? Then add a mean column for each model.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The output should be:

    Model     Final     mean
0   f0901       4       3.0
1   f0901       2       3.0
2   rf0902      4       4.0
3   rf0902      5       4.0
4   rf0902      3       4.0
5   indi0902    4       2.5
6   indi0902    3       2.5
7   indi0902    1       2.5
8   indi0902    2       2.5

>Solution :

You can assign the output of groupby.transform('mean') and reset_index:

out = (df.assign(mean=df.groupby(level=0)['Final'].transform('mean'))
         .reset_index()
       )

Output:

      Model  Final      mean
0     f0901      4  3.000000
1     f0901      2  3.000000
2    rf0902      2  3.333333
3    rf0902      5  3.333333
4    rf0902      3  3.333333
5  indi0902      4  2.500000
6  indi0902      3  2.500000
7  indi0902      2  2.500000
8  indi0902      1  2.500000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading