如何从 Dataframe 中删除两列中的值相同的行?

How to drop rows from Dataframe where values in 2 columns are the sam?

我的DF如下图

Date   New_date X  Y
01-12  01-12    3  4
01-13  01-13    6  1
01-14  01.15    2  3

我需要这个结果:

Date   New_date X  Y
01-14  01.15    2  3

o 代码应删除前 2 行,因为日期列和 New_date 列中的值相同。我试过这个:

df.drop(df.loc[df['Date'] == df['New_date']])

但是没用。有什么想法吗?

致以最诚挚的问候并感谢您的帮助

使用pd.DataFrame.drop_duplicates

df = df.drop_duplicates(subset=['Date', 'New_date'], keep=False)

更改逻辑 - 如果值不相等,则获取所有行。

因此将 == 更改为 != 以获取不相等的值并在 boolean indexing 中过滤:

df = df[df['Date'] != df['New_date']]
print (df)
    Date New_date  X  Y
2  01-14    01.15  2  3