简体   繁体   English

如何从 Dataframe 中删除行,其中 2 列中的值是 sam?

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

My DF looks like below我的 DF 如下所示

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

I need this result:我需要这个结果:

Date   New_date X  Y
01-14  01.15    2  3

o code should remove first 2 rows because values in columns Date and New_date are the same. o 代码应删除前 2 行,因为列 Date 和 New_date 中的值相同。 I tried with this:我试过这个:

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

But it doesn't work.但它不起作用。 Any idea?任何想法?

Best regards and thanks for help最好的问候和感谢您的帮助

Use pd.DataFrame.drop_duplicates使用pd.DataFrame.drop_duplicates

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

Change logic - get all rows if not equal values.更改逻辑 - 如果值不相等,则获取所有行。

So change == to != for not equal values and filter in boolean indexing :因此,将==更改为!=以获取不相等的值并在boolean indexing中过滤:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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