简体   繁体   English

如果每个值都相等,则删除熊猫数据框行

[英]delete pandas dataframe row if every value is equal

If I have a pandas dataframe which has a row containing float values and all the values are equal in the row, how do I delete that row from the dataframe?如果我有一个 pandas 数据框,其中一行包含浮点值,并且该行中的所有值都相等,我该如何从数据框中删除该行?

Use DataFrame.nunique for test number of unique values per rows with Series.ne for filter out unique rows by boolean indexing :使用DataFrame.nunique测试每行唯一值的数量,使用Series.ne通过boolean indexing过滤掉唯一行:

df1 = df[df.nunique(axis=1).ne(1)]

Or test if not equal first column and test if at least one True per rows by DataFrame.any :或者测试是否不等于第一列并通过DataFrame.any测试每行是否至少有一个True

df1 = df[df.ne(df.iloc[:, 0], axis=0).any(axis=1)]

EDIT: If want remove all rows and all columns with same values solution should be changed for test columns with loc and axis=0 :编辑:如果要删除具有相同值的所有行和所有列,则应为具有locaxis=0的测试列更改解决方案:

df = pd.DataFrame({
         'B':[4,4,4,4,4,4],
         'C':[4,4,9,4,2,3],
         'D':[4,4,5,7,1,0],

})

print (df)
   B  C  D
0  4  4  4
1  4  4  4
2  4  9  5
3  4  4  7
4  4  2  1
5  4  3  0

df2 = df.loc[df.nunique(axis=1).ne(1), df.nunique(axis=0).ne(1)]

And for second solution:对于第二种解决方案:

df2 = df.loc[df.ne(df.iloc[:, 0], axis=0).any(axis=1), df.ne(df.iloc[0], axis=1).any(axis=0)]

print (df2)
   C  D
2  9  5
3  4  7
4  2  1
5  3  0

You can use DataFrame.diff over axis=1 (per row):您可以在axis=1 (每行)上使用DataFrame.diff

# Example dataframe:
df = pd.DataFrame({'Col1':[1,2,3],
                   'Col2':[2,2,5],
                   'Col3':[4,2,9]})

   Col1  Col2  Col3
0     1     2     4
1     2     2     2  # <-- row with all same values
2     3     5     9
df[df.diff(axis=1).fillna(0).ne(0).any(axis=1)]

   Col1  Col2  Col3
0     1     2     4
2     3     5     9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM