Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
question from:https://stackoverflow.com/questions/66056039/pandas-checking-true-false-in-groupby-for-last-n-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
487 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...