将大写应用于pandas数据框中的列 [英] Applying uppercase to a column in pandas dataframe

查看:87
本文介绍了将大写应用于pandas数据框中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将大写字母应用于DataFrame中的列时遇到麻烦.

I'm having trouble applying upper case to a column in my DataFrame.

数据帧是df.

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

1/2 ID is the column head that need to apply UPPERCASE.

问题在于值由三个字母和三个数字组成.例如,rrr123是值之一.

The problem is that the values are made up of three letters and three numbers. For example rrr123 is one of the values.

df['1/2 ID'] = map(str.upper, df['1/2 ID'])

我遇到一个错误:

TypeError: descriptor 'upper' requires a 'str' object but received a 'unicode' error.

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

How can I apply upper case to the first three letters in the column of the DataFrame df?

推荐答案

这应该有效:

df['1/2 ID'] = map(lambda x: str(x).upper(), df['1/2 ID'])

,并且您是否希望所有columns名称都使用大写格式:

and should you want all the columns names to be in uppercase format:

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

这篇关于将大写应用于pandas数据框中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆