簡體   English   中英

如何基於給定值獲取列的百分比

[英]How to get percentage of a column based on a given value

我有下面的數據集。 我想要獲取等於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]

但我正在尋找更好的解決方案。 有沒有更簡單的方法可以做到這一點?

使用pd.Series.mean

df.Survived.mean()

0.5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM