本文介绍了在 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 20I 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 中重命名列名
发布评论