简体   繁体   中英

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

My DF looks like below

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. 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

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 :

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

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