Python 如何使用名称列表更改 Pandas Dataframe 中的列名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47343838/
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-08-19 18:10:39  来源:igfitidea点击:

How to change column names in pandas Dataframe using a list of names?

pythonpandasnumpy

提问by koliyat9811

I have been trying to change the column names of a pandas dataframe using a list of names. The following code is being used:

我一直在尝试使用名称列表更改熊猫数据框的列名称。正在使用以下代码:

df.rename(columns = list_of_names, inplace=True)

However I got a Type Error each time, with an error message that says "list object is not callable". I would like to know why does this happen? And What can I do to solve this problem? Thank you for your help.

但是,我每次都遇到类型错误,并显示一条错误消息,指出“列表对象不可调用”。我想知道为什么会发生这种情况?我能做些什么来解决这个问题?感谢您的帮助。

回答by Prakash Palnati

you could use

你可以用

df.columns = ['Leader', 'Time', 'Score']

回答by JGC

Just update the columns attribute:

只需更新列属性:

df.columns = list_of_names

回答by YOBEN_S

If you need rename(lis your list of name need to change to)

如果您需要renamel您的姓名列表需要更改为)

df.rename(columns=dict(zip(df.columns,l)))