Home > Back-end >  Python/Pandas: How to maintain a value in column until a condition is met?
Python/Pandas: How to maintain a value in column until a condition is met?

Time:05-17

In python/pandas, I'm trying to add a column that labels the streets (preflop, flop, turn, and river) in a poker game. This is what my dataset looks like. I'm trying to add the "Street" column.

Event Player Datetime Street
Starting Hand 1 NaN 2022-04-08T03:12:22 Preflop
Bet B 2022-04-08T03:13:22 Preflop
Call C 2022-04-08T03:14:22 Preflop
Fold A 2022-04-08T03:15:22 Preflop
Flop NaN 2022-04-08T03:16:22 Flop
Bet B 2022-04-08T03:17:22 Flop
Call C 2022-04-08T03:18:22 Flop
Turn NaN 2022-04-08T03:19:22 Turn
Bet B 2022-04-08T03:20:22 Turn
Fold C 2022-04-08T03:21:22 Turn
Ending Hand 1 NaN 2022-04-08T03:22:22 Turn
Starting Hand 2 NaN 2022-04-08T03:23:22 Preflop
Check C 2022-04-08T03:24:22 Preflop

I'm having trouble figuring out how fill in the rows between the appropriate events in the "Street column. My current solution only takes the events where "Flop", "Turn", and "River" are explicitly stated. Is there a way to iterate over the rows and maintain a value in a column until a condition is met? I imagine it has something to do with Groupby, but I'm not entirely sure.

Thanks!

CodePudding user response:

When you infer the Street values directly from certain keywords in the Event column, make sure to assign NumPy's missing value placeholder, np.nan, to those fields where you can't infer the value directly.

Then you can use pandas' fillna() method, passing the method='ffill' argument for forward filling, meaning each present value will be propagated to all subsequent NaN values.

def infer_street(event):
    if 'Starting' in event:
        return 'Preflop'
    if 'Flop' in event:
        return 'Flop'
    if 'Turn' in event:
        return 'Turn'
    return np.nan

df['Street'] = df['Event'].apply(infer_street)
df['Street'].fillna(method='ffill', inplace=True)
  • Related