我想删除多列具有相同值的行。我读了this question关于两列,并试图扩展到多列,但我得到一个错误。
以下是一些示例数据,类似于我的数据框:
import pandas as pd
data = [['table1',10,8,7],['table2',3,3,3],['table3',3,8,11],['table4',12,12,12],['table5',13,15,5]]
df = pd.DataFrame(data,columns=['table_name','Attr1','Attr2','Attr3'])
和我想要的结果
res = [['table1',10,8,7],['table3',3,8,11],['table5',13,15,5]]
result = pd.DataFrame(res,columns=['table_name','Attr1','Attr2','Attr3'])
我试过了
[df[df['Attr1'] != df['Attr2'] | df['Attr1'] != df['Attr3'] | df['Attr2'] != df['Attr3']]]
它检索错误
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
有任何想法吗?
使用DataFrame.ne
比较Attr1
列的所有值,并测试True
每行至少一个DataFrame.any
,boolean indexing
的最后一个过滤器:
df = df[df[['Attr1','Attr2','Attr3']].ne(df['Attr1'], axis=0).any(axis=1)]
print (df)
table_name Attr1 Attr2 Attr3
0 table1 10 8 7
2 table3 3 8 11
4 table5 13 15 5
细节:
print (df[['Attr1','Attr2','Attr3']].ne(df['Attr1'], axis=0))
Attr1 Attr2 Attr3
0 False True True
1 False False False
2 False True True
3 False False False
4 False True True
print (df[['Attr1','Attr2','Attr3']].ne(df['Attr1'], axis=0).any(axis=1))
0 True
1 False
2 True
3 False
4 True
dtype: bool
另一种解决方案是DataFrame.nunique
测试的唯一值的数量:
df = df[df[['Attr1','Attr2','Attr3']].nunique(axis=1).ne(1)]
您可以为每个创建条件,然后执行比较:
c1 = df['Attr1'].ne(df['Attr2'])
c2 = df['Attr1'].ne(df['Attr3'])
c3 = df['Attr2'].ne(df['Attr3'])
>>> df[c1 | c2 | c3]
table_name Attr1 Attr2 Attr3
0 table1 10 8 7
2 table3 3 8 11
4 table5 13 15 5
每个条件都是一个表示不等式是否成立的系列,例如:
>>> c1
0 True
1 False
2 True
3 False
4 True
dtype: bool
>>> c1 | c2 | c3
0 True
1 False
2 True
3 False
4 True
dtype: bool
布尔索引的条件是跨越轴1的唯一值的数量必须等于DataFrame
的宽度:
df = df[df.nunique(axis=1).eq(df.shape[1])]
使用df.query:
df = df.query("Attr1 != Attr2 != Attr3")