asj177 asj177 - 1 year ago 103
Python Question

Group by column and get mean of the the group pandas

I have a dataframe like below

Mode Time
Air 2
Sea 4
Air 5
Sea 6

So I want to have the output as

Mode Time
Air 3.5
Sea 5

The output should contain two columns Mode and the Time.

With my Code I dont see the columns coming. Below is my code:


The output of this is:

Series: mode
Air 3.813711
Ocean 14.670060
Parcel 3.036790
Truck 3.097268
Name: remainingShiptime, dtype: float64

Answer Source

Don't select the column:

>>> df1.groupby('Mode').mean()
Air    3.5
Sea    5.0

Edit in response to comment

>>> df1.groupby('Mode').mean().reset_index()
  Mode  Time
0  Air   3.5
1  Sea   5.0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download