gpt4 book ai didi

python - 一列中每次出现的值在另一列中的总和

转载 作者:行者123 更新时间:2023-12-01 08:10:26 25 4
gpt4 key购买 nike

我似乎找不到谷歌搜索这个问题的正确措辞,因为我得到了非常相似但不是正确的答案。

我正忙于处理泰坦尼克号数据集,并且想要计算一个家庭中幸存成员的数量。所以数据集看起来像这样:

+-------------+----------+-----------+-------------+
| PassengerId | Survived | Surname | NumSurvived |
+-------------+----------+-----------+-------------+
| 1 | 0 | Braund | |
| 2 | 1 | Cumings | |
| 3 | 1 | Heikkinen | |
| 4 | 1 | Futrelle | |
| 5 | 0 | Braund | |
| 6 | 0 | Moran | |
| 7 | 0 | Futrelle | |
| 8 | 0 | Braund | |
| 9 | 1 | Cumings | |
+-------------+----------+-----------+-------------+

我需要对 NumSurvived 列中每个姓氏的 Survived 值求和,如下所示:

+-------------+----------+-----------+-------------+
| PassengerId | Survived | Surname | NumSurvived |
+-------------+----------+-----------+-------------+
| 1 | 0 | Braund | 0 |
| 2 | 1 | Cumings | 2 |
| 3 | 1 | Heikkinen | 1 |
| 4 | 1 | Futrelle | 1 |
| 5 | 0 | Braund | 0 |
| 6 | 0 | Moran | 0 |
| 7 | 0 | Futrelle | 1 |
| 8 | 0 | Braund | 0 |
| 9 | 1 | Cumings | 2 |
+-------------+----------+-----------+-------------+

最佳答案

尝试:

df['NumSurvived']=df.groupby('Surname')['Survived'].transform(lambda x: x.eq(1).sum())

打印(df)

   PassengerId  Survived    Surname  NumSurvived
0 1 0 Braund 0
1 2 1 Cumings 2
2 3 1 Heikkinen 1
3 4 1 Futrelle 1
4 5 0 Braund 0
5 6 0 Moran 0
6 7 0 Futrelle 1
7 8 0 Braund 0
8 9 1 Cumings 2

关于python - 一列中每次出现的值在另一列中的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55287982/

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