Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
必须传递一个索引才能重命名-python_Python_Indexing_Rename - Fatal编程技术网

必须传递一个索引才能重命名-python

必须传递一个索引才能重命名-python,python,indexing,rename,Python,Indexing,Rename,我有以下数据帧: 我想使用我定义的函数将列重命名为snake case: def to_snakecase(cols): map_dict = {} for col in cols: map_dict[col] = col.lower().strip().replace(' ', '_') 当我编写代码时: covid_df.rename(to_snakecase(covid_df.columns), axis=1, inplace=True) 我得到错误:必须传递一个索引才能重命

我有以下数据帧:

我想使用我定义的函数将列重命名为snake case:

def to_snakecase(cols):
map_dict = {}
for col in cols:
    map_dict[col] = col.lower().strip().replace(' ', '_')
当我编写代码时:

covid_df.rename(to_snakecase(covid_df.columns), axis=1, inplace=True)
我得到错误:
必须传递一个索引才能重命名


我看了文档,但还没弄明白。请帮忙。谢谢。

首先,您的函数返回
None
,重命名函数找不到索引器

def to_snakecase(cols):
    map_dict = {}
    for col in cols:
        map_dict[col] = col.lower().strip().replace(' ', '_')
    return map_dict
我相信重命名列最有效的方法是在
rename
函数中使用关键字
columns
。 您的代码可以如下所示

covid_df.rename(columns=to_snakecase(covid_df.columns), inplace=True)

谢谢你,马瑞克。问题是函数返回None,因此首先没有索引。非常感谢。