简体   繁体   English

如何用另一个 dataframe 行重命名列名?

[英]How to rename the column names with another dataframe rows?

I have a dataframe with column names: df1我有一个 dataframe 列名:df1

A_01 A_02 B_03 C_04
 0    0    0    1
 1    2    1    0
 0    1    0    3

also, df2:还有,df2:

no. value
01  1103
02  1105
03  1210
04  1306

How to rename df1 columns with the value on df2 like as:如何使用 df2 上的值重命名 df1 列,如下所示:

1103 1105 1210 1306
 0    0    0    1
 1    2    1    0
 0    1    0    3

You need:你需要:

df1.columns = df1.columns.str.split('_').str[1].map(df2.set_index('no.')['value'])

Output: Output:

    1103    1105    1210    1306
0   0         0      0         1
1   1         2      1         0
2   0         1      0         3

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

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