Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas DataFrame.rename unexpected keyword argument "axis" when using mapper

Tags:

python

pandas

Following the pandas docs I tried the following (verbatim out of the docs):

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"

I also tried

df.rename(mapper=str.lower, axis='columns')

but then I get:

TypeError: rename() got an unexpected keyword argument "mapper"

Am I looking at an old version of the docs?

like image 735
Dan Avatar asked Dec 13 '17 18:12

Dan


People also ask

How do I rename axis in pandas?

rename_axis() is used to rename the axes of the index or columns in dataframe. Parameters: mapper : [scalar, list-like, optional] Value to set the axis name attribute.

How do I rename the index or columns of a Pandas DataFrame?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.

How do I change the index name in a data frame?

There is also a new DataFrame method rename_axis available to change the index level names. This DataFrame has one level for each of the row and column indexes. Both the row and column index have no name. Let's change the row index level name to 'names'.

How do I rename a series in pandas?

Pandas Series: rename() function The rename() function is used to alter Series index labels or name. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don't throw an error.


2 Answers

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.

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.

The code you have works just fine on the latest release, but not anything older, because mapper and axis were introduced in 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())))
like image 88
cs95 Avatar answered Oct 12 '22 19:10

cs95


You're probably using python2 with an old version of pandas. The axis parameter isn't implemented yet for you. You have 3 choices. Either remove the axis parameter and explicitly name columns, as shown below, or pass a map to rename(...), or else update your version of python and pandas.

import pandas as pd  
import numpy as np  
import sys 
print(sys.version) 
print(pd.__version__)

#create a dataframe with two columns named Foo and BAR 
df = pd.DataFrame({" Foo": [1, 2, 3], "BAR ": [4, 5, 6]}) 
print(df)

#rename the columns in the dataframe to strip whitespace and be all lowercase: 
df = df.rename(columns={c:c.strip().lower() for c in df.columns}) 
print(df)

#change the column named foo to moo, and change the column named bar to baz 
df = df.rename(columns={"foo": "moo", "bar": "baz"}) 
print(df) 

Which prints:

2.7.16 [GCC 4.9.3]
0.16.2
    Foo  BAR 
0     1     4
1     2     5
2     3     6

   foo  bar
0    1    4
1    2    5
2    3    6

   moo  baz
0    1    4
1    2    5
2    3    6
like image 38
Bubble Bubble Bubble Gut Avatar answered Oct 12 '22 18:10

Bubble Bubble Bubble Gut