Convert whole dataframe from lower case to upper case with Pandas Convert whole dataframe from lower case to upper case with Pandas python python

Convert whole dataframe from lower case to upper case with Pandas


astype() will cast each series to the dtype object (string) and then call the str() method on the converted series to get the string literally and call the function upper() on it. Note that after this, the dtype of all columns changes to object.

In [17]: dfOut[17]:      regiment company deaths battles size0  Nighthawks     1st    kkk       5    l1  Nighthawks     1st     52      42   ll2  Nighthawks     2nd     25       2    l3  Nighthawks     2nd    616       2    mIn [18]: df.apply(lambda x: x.astype(str).str.upper())Out[18]:      regiment company deaths battles size0  NIGHTHAWKS     1ST    KKK       5    L1  NIGHTHAWKS     1ST     52      42   LL2  NIGHTHAWKS     2ND     25       2    L3  NIGHTHAWKS     2ND    616       2    M

You can later convert the 'battles' column to numeric again, using to_numeric():

In [42]: df2 = df.apply(lambda x: x.astype(str).str.upper())In [43]: df2['battles'] = pd.to_numeric(df2['battles'])In [44]: df2Out[44]:      regiment company deaths  battles size0  NIGHTHAWKS     1ST    KKK        5    L1  NIGHTHAWKS     1ST     52       42   LL2  NIGHTHAWKS     2ND     25        2    L3  NIGHTHAWKS     2ND    616        2    MIn [45]: df2.dtypesOut[45]: regiment    objectcompany     objectdeaths      objectbattles      int64size        objectdtype: object


This can be solved by the following applymap method:

df = df.applymap(lambda s: s.lower() if type(s) == str else s)


Loops are very slow instead of using apply function to each and cell in a row, try to get columns names in a list and then loop over list of columns to convert each column text to lowercase.

Code below is the vector operation which is faster than apply function.

for columns in dataset.columns:    dataset[columns] = dataset[columns].str.lower()