Danny W
Danny W

Reputation: 473

Pandas checking True False in Groupby for last n Rows

If any of the last N (3) rows OR current were True in a groupby, then I want the output to be true.

This is how I have been trying to do it, either with transform since its a single column.

import pandas as pd

data = [
    ['False', 'CLE',],
    ['False', 'CLE'],
    ['True', 'CLE'],
    ['False', 'MON'],
    ['False', 'CLE'],
    ['False', 'CLE'],
    ['False', 'CLE'],
    ['False', 'CLE']
]

# Create the pandas DataFrame
df = pd.DataFrame(data,
                  columns=["bool", "city"])

df['x'] = df.groupby(['city'])['bool']\
            .transform(lambda x: x==True & 
                                 x.index\
                                  .isin(x.index[-2:]))

Output should be:

False
False
True
False
True
True
False
False

Upvotes: 1

Views: 654

Answers (1)

ALollz
ALollz

Reputation: 59579

You can take a rolling sum of your bool column within group and then use astype(bool) to set it True if that row, or any of the previous N were True (i.e. any non-zero sum). First we need to make your values Boolean

df['bool'] = df['bool'].map({'False': False, 'True': True})

df['x'] = (df.groupby('city')['bool'].rolling(3, min_periods=0)
             .sum()
             .astype(bool)
             .reset_index(0, drop=True))

    bool city      x
0  False  CLE  False
1  False  CLE  False
2   True  CLE   True
3  False  MON  False
4  False  CLE   True
5  False  CLE   True
6  False  CLE  False
7  False  CLE  False

Upvotes: 3

Related Questions