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

2024-06-25 23:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据集。我想得到存活的百分比,它的值等于1。你知道吗

     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]

但我在寻找更好的解决办法。有没有更简单的方法?你知道吗


Tags: 数据namedftrainjohn百分比mrshape