Convert whole dataframe from lower case to upper case with Pandas

前端 未结 6 800
忘掉有多难
忘掉有多难 2020-12-13 18:12

I have a dataframe like the one displayed below:

# Create an example dataframe about a fictional army
raw_data = {\'regiment\': [\'Nighthawks\', \'Nighthawk         


        
6条回答
  •  攒了一身酷
    2020-12-13 19:01

    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]: df
    Out[17]: 
         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())
    Out[18]: 
         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
    

    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]: df2
    Out[44]: 
         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 [45]: df2.dtypes
    Out[45]: 
    regiment    object
    company     object
    deaths      object
    battles      int64
    size        object
    dtype: object
    

提交回复
热议问题