Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert case of a pandas DataFrame column

Tags:

python

pandas

I have a DataFrame where the case of a particular column is inconsistent and I want to make it consistent:

In [1]: df = pd.DataFrame({"vals":["alpha","Beta","GAMMA"]})

In [2]: df
Out[2]: 
    vals
0  alpha
1   Beta
2  GAMMA

Is there is an easy, pythonic way to replace each value with the str.upper() of the string str ?

like image 321
vy32 Avatar asked Oct 23 '25 05:10

vy32


1 Answers

This should work for you:

df['vals'] = map(lambda x: x.upper(), df['vals'])

Or even:

df['vals'] = df['vals'].apply(lambda x: x.upper())

Ps.: in recent versions you may also:

df['vals'] = df['vals'].str.upper()
like image 130
dot.Py Avatar answered Oct 24 '25 20:10

dot.Py



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!