Reputation: 7536
Given the following DataFrame:
import pandas as pd
d = pd.DataFrame({'Group':['a','a','c','c'],'Value':[1,1,2,2]})
print(d)
Group Value
0 a 1
1 a 1
2 c 2
3 c 2
Since there is no row with a group of 'b', I want to delete any row with a group of 'a'. I only want to delete the 'a' group because there are no rows in the 'b' group.
I know how to get the number of rows in group 'b' like this:
len(d.loc[d['Group']=='b'])
and then do this:
if len(d.loc[d['Group']=='b'])==0:
d=d.loc[d['Group']!='a']
print(d)
Group Value
2 c 2
3 c 2
...but I'm wondering how to work it into the .loc method in one line.
Thanks in advance!
Upvotes: 0
Views: 176
Reputation: 402553
A more intuitive solution to len
would be creating a mask and then using .all
. After that, just reassign to the filtered slice.
if ~(d.Group == 'b').all():
d = d[d.Group != 'a']
d
Group Value
2 c 2
3 c 2
You can also use a host of other query methods like df.query
and df.*loc
.
Upvotes: 1