[英]Convert case of a pandas DataFrame column
I have a DataFrame where the case of a particular column is inconsistent and I want to make it consistent: 我有一个DataFrame,其中特定列的大小写不一致,并且我想使其一致:
In [1]: df = pd.DataFrame({"vals":["alpha","Beta","GAMMA"]})
In [2]: df
Out[2]:
vals
0 alpha
1 Beta
2 GAMMA
Is there is an easy, pythonic way to replace each value with the str.upper()
of the string str
? 有没有一种简单的Python方式将每个值替换为字符串
str
的str.upper()
?
你可以做:
df['vals'] = df['vals'].apply(lambda x: x.upper())
This should work for you: 这应该为您工作:
df['vals'] = map(lambda x: x.upper(), df['vals'])
Or even: 甚至:
df['vals'] = df['vals'].apply(lambda x: x.upper())
Ps.: in recent versions you may also: 附:在最新版本中,您还可以:
df['vals'] = df['vals'].str.upper()
You can make the changes inplace directly on your dataframe: 您可以直接在数据框上进行更改:
df["vals"].apply(lambda x: x.upper())
Here .upper()
will work whether your elements are of type str
or unicode
. 无论您的元素是
str
还是unicode
类型, .upper()
都可以在这里.upper()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.