如何将 pandas 数据框列中每个单词的第一个字母大写?例如,我正在尝试进行以下转换。
Column1 Column1
The apple The Apple
the Pear ⟶ The Pear
Green tea Green Tea
您可以使用str.title http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.title.html:
df.Column1 = df.Column1.str.title()
print(df.Column1)
0 The Apple
1 The Pear
2 Green Tea
Name: Column1, dtype: object
另一种非常相似的方法是str.capitalize https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.capitalize.html,但它仅将第一个字母大写:
df.Column1 = df.Column1.str.capitalize()
print(df.Column1)
0 The apple
1 The pear
2 Green tea
Name: Column1, dtype: object
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)