简体   繁体   English

通过在 Python 中使用循环连接另一个 dataframe 的值来重命名一个 dataframe 的变量

[英]Rename variables of one dataframe by joining the values of another dataframe by using a Loop in Python

I have two dataframe.我有两个 dataframe。 The first dataframe has the directions第一个 dataframe 有方向

df1 df1

   Direction 
0  sb
1  nb
2  wb
3  eb

The second df2 is第二个df2是

   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 的名称并通过使用方向 df 连接来重命名它们,如下所示:

df2 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?这可能吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM