Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将大写字母应用于dataframe中的列_Python_Python 2.7_Pandas - Fatal编程技术网

Python 将大写字母应用于dataframe中的列

Python 将大写字母应用于dataframe中的列,python,python-2.7,pandas,Python,Python 2.7,Pandas,我无法将大写字母应用于数据框中的列 数据帧是df 1/2 ID是需要应用大写的列标题 问题是这些值由三个字母和三个数字组成。例如,rrr123是其中一个值 df['1/2 ID'] = map(str.upper, df['1/2 ID']) 我有一个错误: TypeError:描述符'upper'需要'str'对象,但收到了'unicode'错误。 如何将大写字母应用于数据框df列中的前三个字母?这应该可以: df['1/2 ID'] = map(lambda x: str(x).upper

我无法将大写字母应用于数据框中的列

数据帧是
df

1/2 ID
是需要应用大写的列标题

问题是这些值由三个字母和三个数字组成。例如,
rrr123
是其中一个值

df['1/2 ID'] = map(str.upper, df['1/2 ID'])
我有一个错误:

TypeError:描述符'upper'需要'str'对象,但收到了'unicode'错误。

如何将大写字母应用于数据框
df
列中的前三个字母?

这应该可以:

df['1/2 ID'] = map(lambda x: str(x).upper(), df['1/2 ID'])
如果您希望所有
名称都采用大写格式:

df.columns = map(lambda x: str(x).upper(), df.columns)
str.upper()

unicode.upper()
将需要unicode而不是字符串(或者您得到TypeError:描述符'upper'需要'unicode'对象,但收到'str')

因此,我建议使用duck类型并对每个元素调用
.upper()
,例如

df['1/2 ID'].apply(lambda x: x.upper(), inplace=True)

如果您的pandas版本是最新版本,则可以使用矢量化字符串方法:


此方法在本地不起作用,因此必须重新分配结果。

str是str类的一个方法,需要str不是unicode,
str.upper(u“foo”)->错误
在panads的v0.23中没有
inplace
参数用于
apply
。因此,我们似乎也需要使用此解决方案重新分配列。
df['1/2 ID'] = df['1/2 ID'].str.upper()