根据字典重命名 PANDAS 中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36531675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:01:35  来源:igfitidea点击:

Rename columns in PANDAS based on dictionary

pythondictionarypandas

提问by km1234

I have a dataframe and I would like to rename the columns based on another dataframe that I plan to use as dictionary. For example, what I have as first dataframe is:

我有一个数据框,我想根据我计划用作字典的另一个数据框重命名列。例如,我的第一个数据帧是:

          AAA   BBB   CCC   DDD
 index   
  1       1     2     3     4
  2       5     6     7     8

and as a second dataframe that I would like to use as dictionary:

并作为我想用作字典的第二个数据框:

           val1    val2
  index
    1      AAA      A7
    2      BBB      B0
    3      CCC      C3
    4      DDD      D1

What I would like to get as result is the following:

我想得到的结果如下:

          A7    B0    C3    D1 
 index   
  1       1     2     3     4
  2       5     6     7     8

Initially I thought to reshape the first dataframe to long format, then merge with the dictionary dataframe and then reshape back to wide format. However I think this is quite inefficient, so I would like to use a more efficient way (if one exists). Thank you very much four your help.

最初我想将第一个数据帧重新整形为长格式,然后与字典数据帧合并,然后重新整形为宽格式。但是我认为这是非常低效的,所以我想使用一种更有效的方式(如果存在的话)。非常感谢四位的帮助。

回答by ayhan

df.renamehas a parameter called columnsthat accepts dictionaries:

df.rename有一个参数叫做columns接受字典:

df.rename(columns=dict(zip(df2["val1"], df2["val2"])))

Out:

出去:

    A7  B0  C3  D1
0   1   2   3   4
1   5   6   7   8

It returns a new DataFrame. You can either use inplace=True, or assign it back to the original DataFrame.

它返回一个新的 DataFrame。您可以使用inplace=True,也可以将其分配回原始 DataFrame。

回答by jezrael

I think you can first create dictionaryfrom df2, then create Seriesfrom columns of df1by to_serieswhich you then mapusing dictionary:

我觉得你可以先创建dictionarydf2,然后创建Series从列df1to_series您再map使用dictionary

print df1
       AAA  BBB  CCC  DDD
index                    
1        1    2    3    4
2        5    6    7    8

print df2
      val1 val2
index          
1      AAA   A7
2      BBB   B0
3      CCC   C3
4      DDD   D1

d = df2.set_index('val1').to_dict()
print d['val2']
{'AAA': 'A7', 'BBB': 'B0', 'CCC': 'C3', 'DDD': 'D1'}

df1.columns = df1.columns.to_series().map(d['val2'])
print df1
       A7  B0  C3  D1
index                
1       1   2   3   4
2       5   6   7   8

回答by Joe T. Boka

You can also just use this one line:

您也可以只使用这一行:

df1.columns=[df2['val2']]

Output:

输出:

    A7  B0  C3  D1
0   1   2   3   7
1   5   6   7   8