简体   繁体   中英

Delete rows with equal values in one column and different values in another column

I have a dataframe that has repeated values in one column (col_a) and repeated values in another column (col_b).

I want to select only the rows that have the same value in one column (col_a) but different values in another column (col_b).

Original dataframe

  col_a col_b col_c
0   1    2     1
1   1    2     1
2   3    20    1
3   3    18    1
4   3    20    1
5   3    18    1

Desired dataframe

  col_a col_b col_c
2   3    20    1
3   3    18    1
4   3    20    1
5   3    18    1

I've tried using df.duplicate but it doesn't work because I have duplicate values in both columns. I want to select only the rows that have different values in column b but equal values in column a.

You can do something like this.

df[df["col_a"] != df["col_b"]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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