Home > Software engineering >  drop rows if the two columns have non-missing values but have different values
drop rows if the two columns have non-missing values but have different values

Time:10-14

I have the following df

sender_name receiver_name
NaN         Carl
Carl        Carl
Peter       Carl
Carlos      Matt
Julia       Bernard
Carlos      NaN
Peter       NaN
NaN         Matt
...

I want to drop if sender_name != receiver_name

If i do

df=df.drop(xfers_dex['name_sender'] != xfers_dex['name_receiver'])

It drops all rows with NaN values..

I only want to drop if both columns have non-missing values. so my desired df

sender_name receiver_name
NaN         Carl
Carl        Carl
Carlos      NaN
Peter       NaN
NaN         Matt
...

CodePudding user response:

Another possible solution:

df[(df['sender_name'] == df['receiver_name']) | (df.isnull().any(axis=1))]

Output:

  sender_name receiver_name
0         NaN          Carl
1        Carl          Carl
5      Carlos           NaN
6       Peter           NaN
7         NaN          Matt

CodePudding user response:

Extend the condition so it also checks that values are different than NaN:

df=df.drop((xfers_dex['name_sender'] != xfers_dex['name_receiver']) & (xfers_dex['name_receiver'] != 'NaN') & (xfers_dex['name_sender'] != 'NaN'))
  • Related