I have a df
:
id value
1 10
2 15
1 10
1 10
2 13
3 10
3 20
I am trying to keep only rows that have 1 unique value in column value
so that the result df
looks like this:
id value
1 10
1 10
1 10
I dropped id = 2, 3
because it has more than 1 unique value in column value
, 15, 13 & 10, 20
respectively.
I read this answer.
But this simply removes duplicates whereas I want to check if a given column - in this case column value
has more than 1 unique value.
I tried:
df['uniques'] = pd.Series(df.groupby('id')['value'].nunique())
But this returns nan
for every row since I am trying to fit n
returns on n+m
rows after grouping. I can write a function and apply it to every row but I was wondering if there is a smart quick filter that achieves my goal.