我似乎找不到合适的措词来谷歌搜索这个问题,因为我得到的答案非常相似,但答案不正确。
我正在处理Titanic数据集,并希望对一个家庭中尚存的成员进行求和。因此数据集如下所示:
+-------------+----------+-----------+-------------+
| 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 |
+-------------+----------+-----------+-------------+
答案 0 :(得分:1)
尝试:
Y
print(df)
M3()