我试图在我的数据框中编译每个团队的累计计数,其中团队=df['result']=='W'。 “W”代表胜利,因此我试图计算在下一场比赛之前每队赢了多少场比赛。这是我的密码。你知道吗
df = pd.DataFrame({
'team': ['Inter', 'Barca', 'Psv', 'Totten', 'Psv', 'Barca', 'Inter', 'Totten', 'Totten', 'Psv', 'Inter', 'Barca'],
'result': ['W', 'W', 'L', 'L', 'D', 'W', 'D', 'W', 'W', 'L', 'D', 'D']
})
df['each_played'] = df.groupby('team').cumcount()
df['each_won'] = ???
print(df)
我已经成功地计算了每支球队在赛前打了多少场比赛,但无法让它为df['each_won']工作。你知道吗
期望输出:
team result each_played each_won
0 Inter W 0 0
1 Barca W 0 0
2 Psv L 0 0
3 Totten L 0 0
4 Psv D 1 0
5 Barca W 1 1
6 Inter D 1 1
7 Totten W 1 0
8 Totten W 2 1
9 Psv L 2 0
10 Inter D 2 1
11 Barca D 2 2
我对熊猫很陌生,任何帮助都将不胜感激。你知道吗
你的第二个问题是
cumsum
问题。在GroupBy.apply
调用中需要shift
和cumsum
。你知道吗相关问题 更多 >
编程相关推荐