gpt4 book ai didi

python - 如何根据给定值获取列的百分比

转载 作者:太空宇宙 更新时间:2023-11-04 00:29:05 25 4
gpt4 key购买 nike

我有以下数据集。我想获得值等于 1 的 Survived 百分比。

     Survived                                               Name
0 0 Braund, Mr. Owen Harris
1 1 Cumings, Mrs. John Bradley (Florence Briggs Th...
2 1 Heikkinen, Miss. Laina
3 1 Futrelle, Mrs. Jacques Heath (Lily May Peel)
4 0 Allen, Mr. William Henry
5 0 Moran, Mr. James
6 0 McCarthy, Mr. Timothy J
7 0 Palsson, Master. Gosta Leonard
8 1 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)
9 1 Nasser, Mrs. Nicholas (Adele Achem)

我知道我可以通过这个命令获取值:

train_df.loc[train_df['Survived'] == 1].shape[0]/train_df.shape[0]

但我正在寻找更好的解决方案。有更简单的方法吗?

最佳答案

使用pd.Series.mean

df.Survived.mean()

0.5

关于python - 如何根据给定值获取列的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com