python - 如何用另一个数据框行重命名列名称?

标签 python pandas dataframe rename

我有一个带有列名称的数据框:df1

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

另外,df2:

no. value
01  1103
02  1105
03  1210
04  1306

如何使用 df2 上的值重命名 df1 列,如下所示:

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

最佳答案

您需要:

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

输出:

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

关于python - 如何用另一个数据框行重命名列名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172632/

相关文章:

python multiprocessing - 文本处理

python - Pandas:以每小时的频率正确地重新采样数据

python - Pandas 为每个字符拆分数据框列

Python pip install h5py 遇到 TypeError : unsupported operand type(s) for -=: 'Retry' and 'int'

python - 函数在 500 次迭代后打印 500 个值函数更新值到 listwidget

pandas - 使用 seaborn 绘图时如何为色调参数指定多个变量?

python - 有没有办法制作列表的列表,然后删除该列表中的第一项?

python - 在具有不同索引的两个数据帧上进行 Pandas 计算

python - 在Python中,如何计算列中的唯一值以逐渐增加组内的行数

python - 属性错误: 'module' object has no attribute 'pydebug' on dreamhost after server update