Given a dataframe as follows:
city district year price
0 bj cy 2018 NaN
1 bj cy 2019 6.0
2 sh hp 2018 4.0
3 sh hp 2019 3.0
4 bj hd 2018 7.0
5 bj hd 2019 NaN
How could I groupby city
and district
, and filter rows if price
is NaN
? Thank you.
The output I needed is like this:
city district year price
0 bj cy 2018 NaN
1 bj cy 2019 6.0
2 bj hd 2018 7.0
3 bj hd 2019 NaN
I have tried with df.groupby(['city', 'district']).filter(lambda df: df[df['price'].isnull()])
, but it doesn't work.