update dataframe based on value from another dataframe 1

update dataframe based on value from another dataframe

In [27]:

df.loc[df.Name.isin(df1.Name), ['Nonprofit', 'Education']] = df1[['Nonprofit', 'Education']]
df
Out[27]:
  Name  Nonprofit  Business  Education
0    X          1         1          0
1    Y          1         1          1
2    Z          1         0          1
3    Y          1         1          1

[4 rows x 4 columns]

Here is what the above code is Doing:
1. Create a new dataframe df1 with the columns you want to add to df.
2. Use df.loc to select the rows in df that you want to update.
3. Use df1 to select the columns you want to update.
4. Assign the values from df1 to df.

Similar Posts