如何在熊猫数据框中组合均值和计数值频率?

问题描述 投票:0回答:2

我正在研究Tianic Data set。我正在根据幸存者的头衔以及每个头衔出现的频率检查幸存者的频率。

train[['Title', 'Survived']].groupby(['Title'], as_index=False).mean().sort_values(by='Survived',ascending=False)

enter image description here

train.Title.value_counts(normalize=True)

enter image description here

是否有可能将两个结合在一起,结果我看到一张桌子?我想将以下内容作为我的决赛桌:

enter image description here

我不确定如何使用聚合函数以我想要的方式进行计数和求和。如果您需要更多信息,请告诉我。

python pandas pandas-groupby kaggle
2个回答
0
投票

IIUC做reindex并分配回给他

#df1=train[['Title', 'Survived']].groupby(['Title'], as_index=False).mean().sort_values(by='Survived',ascending=False)
#s=train.Title.value_counts(normalize=True)

df1['Title Freq']=s.reindex(df1.Title).tolist()

0
投票

考虑使用agg和用户定义的方法来命名聚集,因为agg不会作为其自己的方法公开。为避免列名中的title冲突,请在末尾调用Series.values_count,而不要在开头调用Series.values_count

reset_index
© www.soinside.com 2019 - 2024. All rights reserved.