I have a dataframe: df1
SAP_Name SAP_Class SAP_Sec
Avi 5 C
Rison 6 A
Slesh 7 B
San 8 C
Sud 7 B
df2:
Name_Fi Class
Avi 5
Rison 6
Slesh 7
I am trying to match df2 to df1 such that the matching values should have the headers replaced same as df1.
SAP_Name SAP_Class
Ankan 5
Rison 6
Slesh 7
Below is the code which I am using :
d = {}
for col2 in df2.columns:
for col1 in df1.columns:
cond = df2[col2].isin(df1[col1]).all()
if cond:
d[col2] = col1
df2 = df2.rename(columns=d)
print (df2)
I am able to get the desired output in a small file, however My actual file has 112444 rows × 446 columns and the target file to be changed has 3 rows × 35 columns , the code is running for a long long time in this case. Can anyone please help me here?