我在pandas数据框中有一个列,它都是大写字母。我想将此更改为仅首字母大写的单词。
我尝试了以下内容:
<div style="background-color: red; text-align: center; padding: 10px"><button>button</button></div>
这将返回错误:
import pandas as pd
data = pd.read_csv('my_file.csv')
data['field'] = data['field'].title()
有没有一种简单的方法在pandas列上执行这样的字符串操作?
答案 0 :(得分:3)
在这里找到答案:
http://pandas.pydata.org/pandas-docs/stable/text.html
data['field'] = data['field'].str.title()
答案 1 :(得分:2)
使用列表理解的替代解决方案:
data['field'] = [word.title() for word in data['field']
<强>计时强>
df = pd.DataFrame({'field': ['abc', 'def', 'ghi'] * 100000})
%timeit df['field'].str.title()
10 loops, best of 3: 89.3 ms per loop
%timeit [word.title() for word in df['field']]
10 loops, best of 3: 52.6 ms per loop