1

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.

whiteblack
  • 11
  • 2
  • Does this answer your question? [Pandas: select rows where two columns are different](https://stackoverflow.com/questions/48751140/pandas-select-rows-where-two-columns-are-different) – Aven Desta Feb 18 '21 at 19:35
  • No, this example does not answer my question – whiteblack Feb 18 '21 at 19:44

1 Answers1

0

You can do something like this.

df[df["col_a"] != df["col_b"]]
VBJ
  • 673
  • 5
  • 14
  • 24
  • It does not solve the problem. I need the dataframe to return only the rows that have the same value in col_a and different values in col_b – whiteblack Feb 18 '21 at 19:54