I know it is a recurrent topic in StackOverflow, however I'm struggling a lot to find a solution to this case in other posts.
I just want to capitalize the first letter of a sring (descrbing sth) for every srting in df.Description
Could anyone tell why I can't loop over df.Description
in this case?
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
Thanks a lot in advance
The following suggestion from @Marcel M solved this issue:
df.Description = df.Description.str[0].str.upper() + df.Description.str[1:]
The following should work:
df.Description = df.Description.str[0].str.upper() + df.Description.str[1:]
As described in the documentation, you can use the .str
attribute of a column to get access to various string processing methods that emulate standard Python string manipulation, but work on the entire column. df.Description.str[0]
returns a new Series object of the first character of each string. Then .str.upper()
is used to uppercase that character.
A simpler alternative colud be to use capitalize()
or possibly title()
like this:
df.Description = df.Description.str.capitalize()
or
df.Description = df.Description.str.title()
However, in your case, this is probably undesirable because these would incorrectly change "CH4" to "ch4" or "Ch4", respectively.