英文:
rename returns NoneType object
问题
我已尝试了几种不同的方法来重命名我的数据框中的列名,但都没有成功。
我想将列名"Species"更改为"Common Name"。
使用rename()不会更改列名:
> general.rename({'Species': 'Common Name'}, inplace=True)
> general
Species Count
0 Downy Woodpecker 1
1 Northern Flicker 1
2 Eastern Kingbird 2
重新分配变量会返回一个NoneType对象:
> general = pd.read_csv('july_4/general.csv', usecols=['Species', 'Count'])
> general = general.rename({'Species': 'Common Name'}, inplace=True)
> general[0]
Traceback (most recent call last):
Cell In[85], line 1
general[0]
TypeError: 'NoneType' object is not subscriptable
英文:
I have tried several different ways to rename columns in my dataframe and none are working.
I want to change the column name "Species" to read "Common Name".
> general = pd.read_csv('july_4/general.csv', usecols=['Species', 'Count'])
> general
Species Count
0 Downy Woodpecker 1
1 Northern Flicker 1
2 Eastern Kingbird 2
Using rename() does not change the columns:
> general.rename({'Species': 'Common Name'}, inplace=True)
> general
Species Count
0 Downy Woodpecker 1
1 Northern Flicker 1
2 Eastern Kingbird 2
Reassigning the variable returns a NoneType object:
> general = pd.read_csv('july_4/general.csv', usecols=['Species', 'Count'])
> general = general.rename({'Species': 'Common Name'}, inplace=True)
> general[0]
Traceback (most recent call last):
Cell In[85], line 1
general[0]
TypeError: 'NoneType' object is not subscriptable
答案1
得分: 2
None
是在使用inplace
选项时的预期返回值。你的列没有被重命名的原因是将一个字典作为位置参数尝试重命名行。
例如(为简单起见省略了inplace=True
):
>>> import pandas
>>> df = pandas.DataFrame({"A": [1,2,3], "B": [4,5,6]})
>>> df
A B
0 1 4
1 2 5
2 3 6
>>> df.rename({0: 9})
A B
9 1 4
1 2 5
2 3 6
>>> df.rename({"A": "A1"})
A B
0 1 4
1 2 5
2 3 6
>>> df.rename(columns={"A": "A1"})
A1 B
0 1 4
1 2 5
2 3 6
正如你所看到的,如果存在一个名为"A"
的行,它将被重命名。使用columns
关键字参数来重命名列。
英文:
None
is the expected return value when you use the inplace
option. The reason your column is not being renamed is because a dict as a positional argument attempts to rename rows.
For example (omitting inplace=True
for simplicity):
>>> import pandas
>>> df = pandas.DataFrame({"A": [1,2,3], "B": [4,5,6]})
>>> df
A B
0 1 4
1 2 5
2 3 6
>>> df.rename({0: 9})
A B
9 1 4
1 2 5
2 3 6
>>> df.rename({"A": "A1"})
A B
0 1 4
1 2 5
2 3 6
>>> df.rename(columns={"A": "A1"})
A1 B
0 1 4
1 2 5
2 3 6
As you can see, a row named "A"
would be renamed, if it existed. Use the columns
keyword argument to rename columns.
答案2
得分: 1
参数inplace
导致了错误。如果你使用 inplace=True
,那么数据框会就地修改。inplace
返回 None
,因此出现错误。
你可以通过两种方式解决这个问题:
-
general.rename(columns={'Species': 'Common Name'}, inplace=True)
-
general = general.rename(columns={'Species': 'Common Name'})
英文:
The parameter inplace
causes the error. If you use inplace=True
, then dataframe changes in place. inplace
returns None
, hence the error.
You can solve this problem in two ways:
-
general.rename(columns={'Species': 'Common Name'}, inplace=True)
-
general = general.rename(columns={'Species': 'Common Name'})
答案3
得分: 0
你正在使用inplace=True
,这意味着该方法会在原地执行,且返回值为None
。无论如何,你只需要在rename
中指定正确的轴以获取你想要的结果:
In [24]: general.rename({'Species': 'Common Name'}, axis=1)
Out[24]:
Common Name Count
0 Downy Woodpecker 1
1 Northern Flicker 1
2 Eastern Kingbird 2
英文:
you are using inplace=True
, which means the method work in place and it will return None
. In any case, all you want is to specify the correct axis in rename
to get what you want:
In [24]: general.rename({'Species': 'Common Name'}, axis=1)
Out[24]:
Common Name Count
0 Downy Woodpecker 1
1 Northern Flicker 1
2 Eastern Kingbird 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论