Reputation: 77
I have two dataframe. The first dataframe has the directions
df1
Direction
0 sb
1 nb
2 wb
3 eb
The second df2 is
LT Thr RT UT LT.1 Thr.1 RT.1 UT.1 LT.2 Thr.2 RT.2 UT.2
0
1
2
3
I want to be able to loop through the names of df2 and rename them by concatenating using the direction df as follows:
df2
sb_LT sb_Thr sb_RT sb_UT nb_LT nb_Thr nb_RT nb_UT wb_LT wb_Thr wb_RT wb_UT
0
1
2
3
Is this possible?
Upvotes: 1
Views: 100
Reputation: 1144
You can try
new_columns = [df1.iloc[1,0]+"_"+x.split(".")[0] if '1' in x else
df1.iloc[2,0]+"_"+x.split(".")[0] if '2' in x else
df1.iloc[3,0]+"_"+x.split(".")[0] if '3' in x else
df1.iloc[0,0]+"_"+x.split(".")[0] for x in df2]
df2.columns = new_columns
Upvotes: 1