[英]How to drop rows with duplicates column values and the number of columns are not always fixed?
如果您有更多列,则可以使用set()
+ len()
:
x = df[~df.apply(lambda x: len(set(x)), axis=1).eq(1)]
print(x)
印刷:
site1 site2
0 507814 501972
2 508110 508161
编辑:要指定列:
x = df[~df[["site1", "site2"]].apply(lambda x: len(set(x)), axis=1).eq(1)]
print(x)
印刷:
site1 site2 site3
0 507814 501972 508284
2 508110 508161 508098
df
使用:
site1 site2 site3
0 507814 501972 508284
1 529389 529389 508284
2 508110 508161 508098
你可以这样做:
df = df[df.nunique(axis=1) > 1]
这是另一种方式。 如果您的所有站点值都是数字,这应该有效。
df.loc[df.diff(axis=1).sum(axis=1).ne(0)]
使用您的示例,这将过滤site1 == site2
的列:
# first option
df[~df.apply(lambda x: x["site1"] == x["site2"], axis=1)]
# second option
df.query("site1 != site2")
所有选项都为您提供:
site1 site2
0 507814 501972
2 508110 508161
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.