使用映射器时,pandas DataFrame.rename 意外的关键字参数“axis”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47800034/
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
pandas DataFrame.rename unexpected keyword argument "axis" when using mapper
提问by Dan
Following the pandas docsI tried the following (verbatim out of the docs):
在Pandas文档之后,我尝试了以下内容(逐字逐字地从文档中删除):
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
df.rename(str.lower, axis='columns')
yet I'm getting the error
但我收到错误
TypeError: rename() got an unexpected keyword argument "axis"
类型错误:rename() 得到了意外的关键字参数“axis”
I also tried
我也试过
df.rename(mapper=str.lower, axis='columns')
but then I get:
但后来我得到:
TypeError: rename() got an unexpected keyword argument "mapper"
类型错误:rename() 得到了一个意外的关键字参数“mapper”
Am I looking at an old version of the docs?
我在看旧版本的文档吗?
回答by cs95
Am I looking at an old version of the docs?
我在看旧版本的文档吗?
No, quite the opposite, in fact. You're looking at the latest version (0.21
as of now). I'm pretty sure you have an older version of pandas.
不,事实上恰恰相反。您正在查看最新版本(0.21
截至目前)。我很确定你有一个旧版本的Pandas。
In the older version, Some of the functions used axis
to specify index/columns, whereas other functions used index=...
or columns=...
. To alleviate this, the devs have made an overhaul of many of the APIs to make them more consistent with each other. rename
is one of them.
在旧版本中,一些函数用于axis
指定索引/列,而其他函数使用index=...
或columns=...
。为了缓解这种情况,开发人员对许多 API 进行了大修,使它们彼此更加一致。rename
是其中之一。
The code you have works just fine on the latest release, but not anything older, because mapper
and axis
were introduced in 0.21
.
您拥有的代码在最新版本上运行良好,但不是旧版本,因为mapper
和axis
是在0.21
.
For reference, on older versions, the following alternatives all work -
作为参考,在旧版本上,以下替代方案都有效 -
df.columns = df.columns.str.lower()
And,
和,
df = df.rename(columns=dict(zip(df.columns, df.columns.str.lower())))
回答by Bubble Bubble Bubble Gut
Maybe you could pass a map to the columns
:
也许您可以将地图传递给columns
:
df.rename(columns={c:c.lower() for c in df.columns})
Hope it helps!
希望能帮助到你!