Home > other >  merge / concat two dataframes on column values and drop subsequent rows from the resulting dataframe
merge / concat two dataframes on column values and drop subsequent rows from the resulting dataframe

Time:10-05

I have 2 data frames

df1 

| email | ack |
| -------- | -------------- |
| [email protected]    | 1             |
| [email protected]   | 1             |
| [email protected]    | 1             |
| [email protected]   | 1             |
| [email protected]    | 1             |
| [email protected]    | 1             |
| [email protected]  | 1             |
| [email protected]    | 1             |

df2 
| email | ack |name| date|
| -------- | -------------- |-------------- |-------------- |
|[email protected]     | 0            |abc | 01/01/2022 |
| [email protected]   | 0            |xyz | 01/02/2022 |
| [email protected]    | 0            |mno | 01/03/2022 |
| [email protected]   | 0            |pqr | 01/04/2022 |
| [email protected]    | 0            |adam| 01/05/2022 |
| [email protected]    | 0            |eve |01/06/2022|
| [email protected]  | 0            |mary|01/07/2022|
| [email protected]    | 0            |john|01/08/2022|
| [email protected]     | 0            |kate|01/09/2022|
| [email protected]      | 0            |matt|01/10/2022|

How do i merge the above two dataframes so as to replace the values in 'ack' column of df2 wherever applicable i.e., on email address.

result

df2 
| email | ack |name| date|
| -------- | -------------- |-------------- |-------------- |
|[email protected]    | 1            |abc|01/01/2022|
| [email protected]   | 1            |xyz|01/02/2022|
| [email protected]   | 1           |mno|01/03/2022|
| [email protected]   | 1           |pqr|01/04/2022|
| [email protected]   | 1           |adam|01/05/2022|
| [email protected]   | 1            |eve|01/06/2022|
| [email protected]   | 1            |mary|01/07/2022|
| [email protected]   | 1           |john|01/08/2022|
| [email protected]   | 0           |kate|01/09/2022|
| [email protected]   | 0           |matt|01/10/2022|

I tried left join and outer join, it appended rows to existing rows.

CodePudding user response:

Assuming df1['ack'] is always 1, the following code should work:

df2.loc[df2['email'].isin(df1['email']), 'ack'] = 1

In English: If df2['email'] is found in df1['email'], set df2['ack'] = 1

  • Related