pandas 通过熊猫删除两列具有相同值的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43951558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:35:48  来源:igfitidea点击:

Remove rows that two columns have the same values by pandas

pandas

提问by Hyman

Input:

输入:

    S   T   W      U
0   A   A   1   Undirected
1   A   B   0   Undirected
2   A   C   1   Undirected
3   B   A   0   Undirected
4   B   B   1   Undirected
5   B   C   1   Undirected
6   C   A   1   Undirected
7   C   B   1   Undirected
8   C   C   1   Undirected

Output:

输出:

    S   T   W      U
1   A   B   0   Undirected
2   A   C   1   Undirected
3   B   A   0   Undirected
5   B   C   1   Undirected
6   C   A   1   Undirected
7   C   B   1   Undirected

For column S and T ,rows(0,4,8) have same values. I want to drop these rows.

对于列 S 和 T ,rows(0,4,8) 具有相同的值。我想删除这些行。

Trying:

试:

I used df.drop_duplicates(['S','T']but failed, how could I get the results.

我用过df.drop_duplicates(['S','T']但失败了,我怎么能得到结果。

回答by jezrael

You need boolean indexing:

你需要boolean indexing

print (df['S'] != df['T'])
0    False
1     True
2     True
3     True
4    False
5     True
6     True
7     True
8    False
dtype: bool

df = df[df['S'] != df['T']]
print (df)
   S  T  W           U
1  A  B  0  Undirected
2  A  C  1  Undirected
3  B  A  0  Undirected
5  B  C  1  Undirected
6  C  A  1  Undirected
7  C  B  1  Undirected

Or query:

query

df = df.query("S != T")
print (df)
   S  T  W           U
1  A  B  0  Undirected
2  A  C  1  Undirected
3  B  A  0  Undirected
5  B  C  1  Undirected
6  C  A  1  Undirected
7  C  B  1  Undirected