Applying uppercase to a column in pandas dataframe

PythonPandas

Python Problem Overview


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

dataframe is df.

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

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'])

I got an error:

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

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

Python Solutions


Solution 1 - Python

If your version of pandas is a recent version then you can just use the vectorised string method upper:

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

This method does not work inplace, so the result must be assigned back.

Solution 2 - Python

This should work:

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

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

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

Solution 3 - Python

str.upper() wants a plain old Python 2 string

unicode.upper() will want a unicode not a string (or you get TypeError: descriptor 'upper' requires a 'unicode' object but received a 'str')

So I'd suggest making use of duck typing and call .upper() on each of your elements, e.g.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGil5RyanView Question on Stackoverflow
Solution 1 - PythonEdChumView Answer on Stackoverflow
Solution 2 - PythonDi ZouView Answer on Stackoverflow
Solution 3 - PythonbakkalView Answer on Stackoverflow