我正在研究Tianic Data set。我正在根据幸存者的头衔以及每个头衔出现的频率检查幸存者的频率。
train[['Title', 'Survived']].groupby(['Title'], as_index=False).mean().sort_values(by='Survived',ascending=False)
和
train.Title.value_counts(normalize=True)
是否有可能将两个结合在一起,结果我看到一张桌子?我想将以下内容作为我的决赛桌:
我不确定如何使用聚合函数以我想要的方式进行计数和求和。如果您需要更多信息,请告诉我。
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()
考虑使用agg
和用户定义的方法来命名聚集,因为agg
不会作为其自己的方法公开。为避免列名中的title冲突,请在末尾调用Series.values_count
,而不要在开头调用Series.values_count
:
reset_index