简体   繁体   English

将 Pandas Dataframe 的列的名称重命名为另一个 Pandas ZC699575A5E8AFD9E22A7ECC8CAB11AFB30DC6784AZ 的值

[英]Rename the name of the columns of a Pandas Dataframe by the values of another Pandas Dataframe

I would like to rename the columns of my Pandas Dataframe, according to the dictionary located in another Dataframe.根据位于另一个 Dataframe 中的字典,我想重命名我的 Pandas Dataframe 的列。

For example, I have the DF1:例如,我有 DF1:

在此处输入图像描述

And I would like to rename the name of the df1 columns according to this dictionary:我想根据这本字典重命名 df1 列的名称:

在此处输入图像描述

My goal is to automatically obtain a dataframe with the following changes:我的目标是通过以下更改自动获得 dataframe:

在此处输入图像描述

PS. PS。 I would like to rename the column after the word "fields", to keep the format "field.CHANGE_NAME".我想在单词“fields”之后重命名该列,以保持格式“field.CHANGE_NAME”。

### data for the first dataframe 
data1 = {
"field.customfield_1032": [48,30,28,38],
"field.customfield_1031": ["RSA", "RSB", "RSC", "RSD"],
"field.customfield_1030": ["Thornton", "MacGyver", "Dalton", "Murdoc "]
 }

df1 = pd.DataFrame(data1)
print(df1)


### data for the second dataframe, this is the diccionary with the data
data2 = {"field.ID": ['field.customfield_1030','field.customfield_1031','field.customfield_1032'], 
    "field.Name": ['Surname','ID', 'Age'],}

df2 = pd.DataFrame(data2)
print(df2)

How could I achieve this?我怎么能做到这一点?

Thank you in advance.先感谢您。

Map the columns to the dict from the combination of df2['field.ID'] & df2['field.Name'] Map df2['field.ID'] & df2['field.Name']组合的字典列

Here is the solution:这是解决方案:

df1.columns = df1.columns.map({x: f"field.{y}" for x, y in zip(df2['field.ID'], df2['field.Name'])})
print(df1)
   field.Age field.ID field.Surname
0         48      RSA      Thornton
1         30      RSB      MacGyver
2         28      RSC        Dalton
3         38      RSD       Murdoc 

Let's try我们试试看

out = df1.rename(columns=dict(df2.values)).add_prefix('field.')
print(out)

   field.Age field.ID field.Surname
0         48      RSA      Thornton
1         30      RSB      MacGyver
2         28      RSC        Dalton
3         38      RSD       Murdoc

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

相关问题 使用字典中的值重命名 Pandas Dataframe 中的列 - Rename Columns in a Pandas Dataframe with values form dictionary Pandas 将 dataframe 的列重命名为另一个 dataframe 的值,如果两个 Z6A8064B53C47945557755705 列的值匹配 - Pandas rename column of dataframe to value of another dataframe if values of two dataframe columns match 将熊猫数据框名称列与另一个数据框的列匹配? - Match pandas dataframe name columns to another dataframe's columns? 根据另一个数据帧将列添加到 Pandas 数据帧并将值设置为零 - Add columns to Pandas dataframe based on another dataframe and set values to zero 根据来自另一个数据框的数据将值分配给Pandas数据框中的列 - Assign values to columns in Pandas Dataframe based on data from another dataframe 子集根据另一个数据帧的值在多个列上进行pandas数据帧 - Subset pandas dataframe on multiple columns based on values from another dataframe 将值添加到基于另一个 dataframe 的 pandas dataframe 列 - adding values to pandas dataframe columns based on another dataframe Pandas Series.rename未反映在DataFrame列中 - Pandas Series.rename not reflected in DataFrame columns 熊猫Goup和重命名DataFrame列名称 - pandas Goup and Rename DataFrame Columns names 重命名Pandas数据框中的几个未命名的列 - Rename several unnamed columns in a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM