[英]Drop a group of rows if one column has missing data in a pandas dataframe
I have the following dataframe:我有以下 dataframe:
df
Group Dist
0 A 5
1 B 2
2 A 3
3 B 1
4 B 0
5 A 5
I am trying to drop all rows that match Group
if the Dist
column equals zero.如果Dist
列为零,我将尝试删除与Group
匹配的所有行。 This works to delete row 4:这适用于删除第 4 行:
df = df[df.Dist != 0]
however I also want to delete rows 1 and 3 so I am left with:但是我也想删除第 1 行和第 3 行,所以我只剩下:
df
Group Dist
0 A 5
2 A 3
5 A 5
Any ideas on how to drop the group based off this condition?关于如何根据这种情况删除组的任何想法?
Thanks!谢谢!
First get all Group
values for Entry == 0
and then filter out them by check column Group
with inverted mask by ~
:首先获取Entry == 0
的所有Group
值,然后通过检查带有反转掩码的列Group
通过~
过滤掉它们:
df1 = df[~df['Group'].isin(df.loc[df.Dist == 0, 'Group'])]
print (df1)
Group Dist
0 A 5
2 A 3
5 A 5
Or you can use GroupBy.transform
with GroupBy.all
for test if groups has no 0
values:或者您可以使用GroupBy.transform
和GroupBy.all
来测试组是否没有0
值:
df1 = df[(df.Dist != 0).groupby(df['Group']).transform('all')]
EDIT: For remove all groups with missing values:编辑:对于删除所有缺少值的组:
df2 = df[df['Dist'].notna().groupby(df['Group']).transform('all')]
For test missing values:对于测试缺失值:
print (df[df['Dist'].isna()])
if return nothing there are no missing values NaN
or no None
like Nonetype.如果什么都不返回,则没有缺失值NaN
或没有None
之类的 Nonetype。
So is possible check scalar, eg if this value is in row with index 10
:因此可以检查标量,例如,如果该值在索引为10
的行中:
print (df.loc[10, 'Dist'])
print (type(df.loc[10, 'Dist']))
You can use groupby
and the method filter
:您可以使用groupby
和方法filter
:
df.groupby('Group').filter(lambda x: x['Dist'].ne(0).all())
Output: Output:
Group Dist
0 A 5
2 A 3
5 A 5
If you want to filter out groups with missing values:如果要过滤掉具有缺失值的组:
df.groupby('Group').filter(lambda x: x['Dist'].notna().all())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.