Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying uppercase to a column in pandas dataframe

Tags:

python

pandas

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?

like image 736
Gil5Ryan Avatar asked Jul 07 '15 12:07

Gil5Ryan


People also ask

How do you capitalize pandas?

capitalize() used to capitalize the string elements in pandas series. Series is a type of data structure which is used as same as list in python. Series can contain the different types of data as we want to enter in the series like list.

How do I make the first letter capital in pandas?

How do you capitalize the first letter of a string? The first letter of a string can be capitalized using the capitalize() function. This method returns a string with the first letter capitalized. If you are looking to capitalize the first letter of the entire string the title() function should be used.


1 Answers

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.

like image 88
EdChum Avatar answered Oct 06 '22 10:10

EdChum