简体   繁体   中英

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. 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? For example:

Detail           Quarter
Shot by...          1
Rebound...          1
End of 1st Quarter  1
Pass to...          2

use a boolean with cumsum and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM