Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Pandas dataframe intersection with varying groups in python ?

By M.K. Verma · 3 May 2022

Pandas dataframe intersection with varying groups in python ?

There are many ways to define the filter you're asking for:

df.groupby('id').filter(lambda x: len(x) > 4)
    # OR
df.groupby('id').filter(lambda x: x['time'].eq(0.4).any())
    # OR
df.groupby('id').filter(lambda x: x['time'].max() == 0.4)

Output:

    time  id   angle
0    0.0  a1   33.67
2    0.0  c3   42.01
4    0.1  a1   12.15
6    0.1  c3   33.12
7    0.2  a1   65.28
8    0.2  c3   87.43
9    0.3  a1   98.85
10   0.3  c3  100.12
11   0.4  a1   11.11
12   0.4  c3   83.22