[英]Increase column value by one based on condition in Pandas
I created a script that scrapes NBA play-by-play data and organizes it into a pandas dataframe.我创建了一个脚本,它会抓取 NBA 的比赛数据并将其组织成 pandas dataframe。 At the end of each quarter and the end of the game, the below values are shown in the 'Detail' column:在每节结束和比赛结束时,“详细信息”列中显示以下值:
Detail
End of the 1st Quarter
End of the 2nd Quarter
End of the 3rd Quarter
End of the 4th Quarter
End of Game
Is there a way to create a 'Quarter' column that starts with the number 1, then increases by 1 after the end of each quarter by using the 'Detail' Column?有没有办法创建一个以数字 1 开头的“季度”列,然后使用“详细信息”列在每个季度结束后增加 1? For example:例如:
Detail Quarter
Shot by... 1
Rebound... 1
End of 1st Quarter 1
Pass to... 2
use a boolean with cumsum
and bfill
使用带有cumsum
和 bfill 的bfill
import numpy as np
df['Quarter'] = np.where(
df['Detail'].str.contains('Quarter'),
df['Detail'].str.contains('Quarter').cumsum(),
np.nan
)
df['Quarter'] = df['Quarter'].bfill()
print(df)
Detail Quarter
0 Shot by... 1.0
1 Rebound... 1.0
2 End of 1st Quarter 1.0
3 Pass to... 2.0
4 Shot by... 2.0
5 Rebound... 2.0
6 End of 2nd Quarter 2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.