在 Pandas 中重命名列名 [英] Renaming column names in Pandas

查看:58
本文介绍了在 Pandas 中重命名列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Pandas 和列标签的 DataFrame,我需要对其进行编辑以替换原始列标签.

我想更改 DataFrame A 中的列名,其中原始列名是:

['$a', '$b', '$c', '$d', '$e']

['a', 'b', 'c', 'd', 'e'].

我将编辑后的列名存储在一个列表中,但我不知道如何替换列名.

解决方案

只需将其分配给 .columns 属性:

<预><代码>>>>df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})>>>df$a $b0 1 101 2 20>>>df.columns = ['a', 'b']>>>df乙0 1 101 2 20

I have a DataFrame using Pandas and column labels that I need to edit to replace the original column labels.

I'd like to change the column names in a DataFrame A where the original column names are:

['$a', '$b', '$c', '$d', '$e']

to

['a', 'b', 'c', 'd', 'e'].

I have the edited column names stored it in a list, but I don't know how to replace the column names.

解决方案

Just assign it to the .columns attribute:

>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
   $a  $b
0   1  10
1   2  20

>>> df.columns = ['a', 'b']
>>> df
   a   b
0  1  10
1  2  20

这篇关于在 Pandas 中重命名列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆