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

Question:

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:

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?

Asked By: 39fredy

||

Answers:

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



Answered By: Naveed
for index, row in old_df.iterrows():
    username = row['username']
    try:
        new_loan = new_df.loc[new_df['username'] == username, 'loan'].values[0]
        old_df['loan'].at[index] = new_loan
    except IndexError:  # If username isn't present in new_df
        continue
Answered By: 0x5961736972
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.