简体   繁体   English

如果 pandas dataframe 中的一列缺少数据,则删除一组行

[英]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.transformGroupBy.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.

相关问题 将Pandas数据帧分组一列,根据另一列删除行 - Group Pandas dataframe by one column, drop rows based on another column 熊猫:更新其中一个列缺少数据的行的数据框值 - Pandas: Updating dataframe values for rows where one colum has missing data Pandas 如果只有第一列有数据,则删除行 - Pandas drop rows if only first column has data 如何基于具有不同行数的另一个 Dataframe 中的一个相似列删除一个 DataFrame 中的行 - How to drop rows in one DataFrame based on one similar column in another Dataframe that has a different number of rows 为 Pandas DataFrame 中的另一列分组的缺失数据添加行 - Add rows for missing data grouped by another column in Pandas DataFrame Pandas 在一个 dataframe 中删除与另一个 dataframe 的列中的行共享一个共同值的行 - Pandas drop rows in one dataframe that share a common value with a rows in a column of another dataframe 一列熊猫数据框丢失数据 - Missing data in a column of pandas dataframe pandas:如果组的最后一行具有特定的列值,如何删除组的所有行 - pandas: how to drop all rows of a group if the last row of the group has certain column value 按列值删除 Pandas DataFrame 中的行(文本) - Drop rows in Pandas DataFrame by Column values (text) 如何在熊猫数据框中的列中删除带有“nan”的行? - how to drop rows with 'nan' in a column in a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM