Python: When looping and adding a string to another got --> TypeError: 'float' object is not subscriptable
Python: When looping and adding a string to another got --> TypeError: 'float' object is not subscriptable
我知道这是 Whosebug 中反复出现的主题,但我在其他帖子中费尽心思寻找解决此问题的方法。
我只想将 df.Description
中每个 srting 的首字母大写(描述某物)
有人能告诉我为什么在这种情况下我不能循环 df.Description
吗?
for idx in df.Description.index:
df.Description.iloc[idx] = df.Description.iloc[idx][0].capitalize() \
+ df.Description.iloc[idx][1:]
df.head()
Out[22]:
Type of Technology ... Sector
0 CF4_TCE ... Dummy
1 CH4_TCE ... Dummy
2 CH4g_TCE ... Dummy
3 CH4n_TCE ... Dummy
4 CH4o_TCE ... Dummy
[5 rows x 7 columns]
df.Description
Out[24]:
0 Tetrafluoromethane (CF4) Total Carbon Emissions
1 Methane total carbon equivalent emissions
2 CH4 emissions from animals directly in Total C...
3 CH4 emissions from anaerobic waste decompostio...
4 Dummy technology converting CH4 emissions from...
...
362 conservation cost curve step for transport demand
363 conservation cost curve step for transport demand
364 conservation cost curve step for transport demand
365 conservation cost curve step for transport demand
366 joint diffusion constraint for transport conse...
Name: Description, Length: 367, dtype: object
非常感谢
@Marcel M 的以下建议解决了这个问题:
df.Description = df.Description.str[0].str.upper() + df.Description.str[1:]
以下应该有效:
df.Description = df.Description.str[0].str.upper() + df.Description.str[1:]
作为 described in the documentation,您可以使用列的 .str
属性来访问模拟标准 Python 字符串操作的各种字符串处理方法,但对整个列起作用. df.Description.str[0]
returns 每个字符串的第一个字符的新 Series 对象。然后 .str.upper()
用于将该字符大写。
一个更简单的替代方法是像这样使用 capitalize()
or possibly title()
:
df.Description = df.Description.str.capitalize()
或
df.Description = df.Description.str.title()
但是,在您的情况下,这可能是不可取的,因为它们会错误地将 "CH4" 分别更改为 "ch4" 或 "Ch4"。
我知道这是 Whosebug 中反复出现的主题,但我在其他帖子中费尽心思寻找解决此问题的方法。
我只想将 df.Description
有人能告诉我为什么在这种情况下我不能循环 df.Description
吗?
for idx in df.Description.index:
df.Description.iloc[idx] = df.Description.iloc[idx][0].capitalize() \
+ df.Description.iloc[idx][1:]
df.head()
Out[22]:
Type of Technology ... Sector
0 CF4_TCE ... Dummy
1 CH4_TCE ... Dummy
2 CH4g_TCE ... Dummy
3 CH4n_TCE ... Dummy
4 CH4o_TCE ... Dummy
[5 rows x 7 columns]
df.Description
Out[24]:
0 Tetrafluoromethane (CF4) Total Carbon Emissions
1 Methane total carbon equivalent emissions
2 CH4 emissions from animals directly in Total C...
3 CH4 emissions from anaerobic waste decompostio...
4 Dummy technology converting CH4 emissions from...
...
362 conservation cost curve step for transport demand
363 conservation cost curve step for transport demand
364 conservation cost curve step for transport demand
365 conservation cost curve step for transport demand
366 joint diffusion constraint for transport conse...
Name: Description, Length: 367, dtype: object
非常感谢
@Marcel M 的以下建议解决了这个问题:
df.Description = df.Description.str[0].str.upper() + df.Description.str[1:]
以下应该有效:
df.Description = df.Description.str[0].str.upper() + df.Description.str[1:]
作为 described in the documentation,您可以使用列的 .str
属性来访问模拟标准 Python 字符串操作的各种字符串处理方法,但对整个列起作用. df.Description.str[0]
returns 每个字符串的第一个字符的新 Series 对象。然后 .str.upper()
用于将该字符大写。
一个更简单的替代方法是像这样使用 capitalize()
or possibly title()
:
df.Description = df.Description.str.capitalize()
或
df.Description = df.Description.str.title()
但是,在您的情况下,这可能是不可取的,因为它们会错误地将 "CH4" 分别更改为 "ch4" 或 "Ch4"。