close

How to calculate mean values grouped on another column in Pandas

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to calculate mean values grouped on another column in Pandas in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to calculate mean values grouped on another column in Pandas?

  1. How to calculate mean values grouped on another column in Pandas?

    You could groupby on StationID and then take mean() on BiasTemp. To output Dataframe, use as_index=False

  2. calculate mean values grouped on another column in Pandas

    You could groupby on StationID and then take mean() on BiasTemp. To output Dataframe, use as_index=False

Method 1

You could groupby on StationID and then take mean() on BiasTemp. To output Dataframe, use as_index=False

In [4]: df.groupby('StationID', as_index=False)['BiasTemp'].mean()
Out[4]:
  StationID  BiasTemp
0        BB       5.0
1     KEOPS       2.5
2    SS0279      15.0

Without as_index=False, it returns a Series instead

In [5]: df.groupby('StationID')['BiasTemp'].mean()
Out[5]:
StationID
BB            5.0
KEOPS         2.5
SS0279       15.0
Name: BiasTemp, dtype: float64

Method 2

This is what groupby is for:

In [117]:
df.groupby('StationID')['BiasTemp'].mean()

Out[117]:
StationID
BB         5.0
KEOPS      2.5
SS0279    15.0
Name: BiasTemp, dtype: float64

Here we groupby the ‘StationID’ column, we then access the ‘BiasTemp’ column and call mean on it

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read