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
?
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With