Federico Gentile Federico Gentile - 1 year ago 1472
Python Question

Convert whole dataframe from lower case to upper case with Pandas

I have a dataframe like the one displayed below:

# Create an example dataframe about a fictional army
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks'],
'company': ['1st', '1st', '2nd', '2nd'],
'deaths': ['kkk', 52, '25', 616],
'battles': [5, '42', 2, 2],
'size': ['l', 'll', 'l', 'm']}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size'])

enter image description here

My goal is to transform every single string inside of the dataframe to upper case so that it looks like this:

enter image description here

Notice: all data types are objects and must not be changed; the output must contain all objects. I want to avoid to convert every single column one by one... I would like to do it generally over the whole dataframe possibly.

What I tried so far is to do this but without success


Answer Source

astype() will cast each series to dtype string (object) and then call the str() method on the converted series to get the string literally and call the function upper() on it.

In [17]: df
     regiment company deaths battles size
0  Nighthawks     1st    kkk       5    l
1  Nighthawks     1st     52      42   ll
2  Nighthawks     2nd     25       2    l
3  Nighthawks     2nd    616       2    m

In [18]: df.apply(lambda x: x.astype(str).str.upper())
     regiment company deaths battles size
0  NIGHTHAWKS     1ST    KKK       5    L
1  NIGHTHAWKS     1ST     52      42   LL
2  NIGHTHAWKS     2ND     25       2    L
3  NIGHTHAWKS     2ND    616       2    M
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download