在 Pandas 中重命名列名

编程入门 行业动态 更新时间:2024-10-24 03:23:15
本文介绍了在 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 中重命名列名

本文发布于:2023-10-16 16:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   Pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!