I have two dataframes, df1 and df2:
Now, want to rename the column name of the df1 as per the row of df2. Note the differing column names order. Only matching column's rows to be taken and the expected output is:
Reproducible inputs:
data1={'a': {'i1': 1, 'i2': 0, 'i3': 1, 'i4': 2}, 'b': {'i1': 0, 'i2': 2, 'i3': 3, 'i4': 4}, 'c': {'i1': 1, 'i2': 1, 'i3': 2, 'i4': 3}, 'd': {'i1': 0, 'i2': 0, 'i3': 0, 'i4': 0}, 'e': {'i1': 5, 'i2': 5, 'i3': 5, 'i4': 5}, 'f': {'i1': 3, 'i2': 4, 'i3': 5, 'i4': 3}}
df1 = pd.DataFrame(data1)
data2={'b': {0: '2z'}, 'a': {0: 'u2'}, 'f': {0: 't6y'}, 'c': {0: '7uw'}, 'z': {0: '9mn'}, 'e': {0: 'ui9'}, 'd': {0: 're3'}}
df2 = pd.DataFrame(data2)
What I tried both with pd.merge
and pd.concat
but both unfortunately did not help me to reach the output. When I appended new row from df2, and then tried to rename the column name but this resulted into duplicate index error.
Any help will be appreciated.
CodePudding user response:
column_names = df2.to_dict('records')
df1.columns = [column_names[0][i] for i in df1.columns]
u2 | 2z | 7uw | re3 | ui9 | t6y | |
---|---|---|---|---|---|---|
i1 | 1 | 0 | 1 | 0 | 5 | 3 |
i2 | 0 | 2 | 1 | 0 | 5 | 4 |
i3 | 1 | 3 | 2 | 0 | 5 | 5 |
i4 | 2 | 4 | 3 | 0 | 5 | 3 |
CodePudding user response:
you can use the below it will help for sure
df1 = df1.rename(columns=table.set_index('df1').df2)
CodePudding user response:
Try this:
df1.rename(columns=df2.iloc[0], inplace=True)