如何根据Pandas中的其他列值计算每个年龄的平均值

问题描述投票：0回答：3

``````df_edad_estado_civil.groupby(['Estado_Civil', 'Edad'], as_index=False).mean()
``````

``````DataError: No numeric types to aggregate
``````

``````
0   38  Soltero
2   26  Soltero
4   38  Soltero
5   24  Soltero
6   28  Soltero
8   32  Soltero
9   19  Soltero
10  28  Soltero
12  27  Soltero
15  38  Soltero
``````

``````years_old  status_mediam
34         single
.            .
36          single
37          married
38         married ....
45         divorced and so on.
``````
python jupyter-notebook pandas-groupby data-science mean
3个回答
0

`Estado_Civil`列转换为数字类型，其中0表示已婚，1表示单身，其中的内容为“df.Estado_Civil = df.Estado_Civil.apply（lambda x：1 *（x =='single'））。然后执行groupby。

0

``````data = [[38, 'Soltero'],
[26, 'Soltero'],
[38, 'Soltero'],
[24, 'Soltero'],
[28, 'Soltero'],
[19, 'Soltero'],
[28, 'Soltero'],
[27, 'Soltero'],
[38, 'Soltero']]

print (result)
``````

``````    Estado_Civil     Edad
0             38  Soltero
2             26  Soltero
3             38  Soltero
4             24  Soltero
5             28  Soltero
7             19  Soltero
8             28  Soltero
10            27  Soltero
13            38  Soltero
Soltero    29.555556
``````

0

``````df_edad_estado_civil.groupby('Edad')['Estado_Civil'].agg(pd.Series.mode)
``````