Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace dataframe values by another data frame where username are the same

I have the following dataframes:

>>> old_data_df
    username  loan_balance  age
0  username1            10   18
1  username7            14    8

and

>>> new_data_df
    username  loan_balance
0  username1             1
1  username2             4

I want to replace the values of loan balance of old_data_df by the values of new_data_df where:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

old_data_df['username'] == new_data_df['username']

The desired outcome would be:

>>> old_data_df
    username  loan_balance  age
0  username1            1   18
1  username7            14    8

What would be a good way of doing this?

>Solution :

you can update using map


#set_index to uername in new_df and then map loan_balance value
old_df['loan_balance'] = (old_df['username'].
                          map(new_df.set_index(['username'])['loan_balance']))
old_df

username    loan_balance    age
0   username1   1   18

alternately,
if you only want to update balance when value exists in new_df

#using mask check if mapping exists
# if so, update, else leave value as is

old_df['loan_balance'] = old_df['loan_balance'].mask(
    (old_df['username'].map(new_df.set_index(['username'])['loan_balance'])).notna(),
    (old_df['username'].map(new_df.set_index(['username'])['loan_balance']))
)
old_df



Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading