'Partially update a dataframe based on selected rows and columns from another
I have two dataframes as follows:
df1
Name Id c1 c2 c3 c4
---------------------------
asd 101 a b c d
cdf 231 e ? 1
zxs 342 f o
ygg 521 g k p
mlk 432 h m z
abc 343 c x q
xyz 254 1 d 2
fgg 165 c z d mm
mnd 766 2 d v
df2
df2_Name df2_Id df2_c2 df2_c4
----------------------------------
asd 101 h d2
ygg 521 x cd
fgg 165 o cm
I want to match the "Name" and "id" from df1 with "df2_Name" and "df2_id" of df2. Wherever a match is found, the values of "c2" and "c4" in df1 are replaced by the values in "df2_c2" and "df2_c4" from df2.
Desired output
Name Id c1 c2 c3 c4
-------------------------------
asd 101 a h c d2
cdf 231 e ? 1
zxs 342 f o
ygg 521 g x p cd
mlk 432 h m z
abc 343 c x q
xyz 254 1 d 2
fgg 165 c o d cm
mnd 766 2 d v
Tried solution 1
df1[df1.set_index(['Name', 'id']).index.isin(df2.set_index(['df2_Name','df2_id']).index)].iloc[:,[3,5]].update(df2.iloc[:,[2,3]])
Result: Original df1 is returned as it is.
Tried solution 2
df1.loc[df1.set_index(['Name', 'id']).index.isin(df2.set_index(['df2_Name','df2_id']).index), ['c2', 'c4']] = df2[['df2_c2', 'df2_c4']]
Result: NaNs introduced
Name id c1 c2 c3 c4
----------------------------
asd 101 a NaN c NaN
cdf 231 e ? 1
zxs 342 f o
ygg 521 g NaN p NaN
mlk 432 h m z
abc 343 c x q
xyz 254 1 d 2
fgg 165 c NaN d NaN
mnd 766 2 d v
Tried solution 3 (for c2 only)
merged = df1.merge(df2, left_on=["id", "Name"], right_on=["df2_id", "df2_Name"])
merged["c2"] = merged.apply(lambda x: x["c2"] if pd.isnull(x["df2_c2"]) else x["df2_c2"], axis=1)
Result:
Name id c1 c2 c3 c4 df2_Name df2_id df2_c2 df2_c4
--------------------------------------------------------------
asd 101 a h c d asd 101 h d2
ygg 521 g x p ygg 521 x cd
fgg 165 c o d mm fgg 165 o cm
This solution 3 replaces the values of selected columns, however it returns the merged dataframe and not the entire df1 with updates.
Can anyone help me with this problem?
Note:
This question is being asked after trying the solutions in the following, however there is no success:
Solution 1:[1]
# Excel file name df1_df2.xlsx with 2 sheets name df1 & df2
# In df2 the column names are 'Name' 'Id' 'c_2' 'c_4'
# In df1 the column names are 'Name' 'Id' 'c1' 'c2' 'c3' 'c4'
import pandas as pd
import openpyxl
import xlsxwriter
url = "df1_df2.xlsx"
df = pd.ExcelFile(url)
df1 = df.parse('df1')
df2 = df.parse('df2')
merged = pd.merge(df1,df2, how='outer', on=['Id'])
merged["c2"] = merged.apply(lambda x: x["c2"] if pd.isnull(x["c_2"])
else x["c_2"], axis=1)
merged.reindex(['Name','Id','c1','c2','c3','c4'], axis=1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
