[英]How to get mean for each sepeartely grouped items in a dataframe uisng pandas?
import numpy as np
import pandas as pd
df=pd.DataFrame({'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'],
'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4],
'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2],
'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']},index= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
df
From this I need to calculate the mean age for each different birds in dataframe.由此我需要计算数据框中每只不同鸟类的平均年龄。
Here is my code这是我的代码
df1=df.get_group('Cranes')
print(df1)
df1[['age']].mean()
df3=df.get_group('plovers')
print(df3)
df3.mean()
df4=df.get_group('spoonbills')
print(df4)
df4.mean()
Output I am getting is
birds age visits priority
a Cranes 3.5 2 yes
b Cranes 4.0 4 yes
f Cranes 3.0 4 no
h Cranes NaN 2 yes
birds age visits priority
c plovers 1.5 3 no
g plovers 5.5 2 no
birds age visits priority
d spoonbills NaN 4 yes
e spoonbills 6.0 3 no
i spoonbills 8.0 3 no
j spoonbills 4.0 2 no
Out[33]:出[33]:
age 6.0
visits 3.0
dtype: float64
which is mean of age from all groups.But I need the mean to get displayed under each group such as mean for cranes , mean for plovers and mean for spoonbills seperatley .这是所有组的平均年龄。但我需要在每个组下显示平均数,例如鹤的平均数,千鸟的平均数和琵鹭的平均数。 What modification is needed to the code ?代码需要做哪些修改? Kinldy help好帮手
As the comments suggest you can use groupby
and then mean
正如评论建议您可以使用groupby
然后mean
To get a Pandas Series:要获得 Pandas 系列:
df.groupby('birds').mean()['age']
Results in:结果是:
birds
Cranes 3.5
plovers 3.5
spoonbills 6.0
Name: age, dtype: float64
To get a DataFrame, you can reset the index:要获取 DataFrame,您可以重置索引:
df.groupby('birds').mean()['age'].reset_index()
Resulting in:导致:
birds age
0 Cranes 3.5
1 plovers 3.5
2 spoonbills 6.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.