Skip to content Skip to sidebar Skip to footer

Pandas Group By Window Range (follow Up Question With Category)

Follow up question: I have the following data table: I want to extract groups within a certain window and category, for example windows_size= 1000000 value category 65

Solution 1:

Use custom lambda function:

window_size = 1000000
f = lambda x: x.diff().abs().gt(window_size).cumsum()
df["group"] = df.groupby('category')["value"].apply(f)+1
print (df)
        value category  group
0  65951649.0        A      1
1  59397882.0        A      2
2   7633231.0        A      3
3   7638485.0        A      3
4  65951649.0        B      1
5  59397882.0        B      2
6   7633231.0        B      3
7   7638485.0        B      3

Or double groupby, because need difference and cumulative sum per groups:

df["group"] = (df.groupby('category')["value"].diff().abs().gt(window_size)
                 .groupby(df['category']).cumsum()+1)

Post a Comment for "Pandas Group By Window Range (follow Up Question With Category)"