Convert whole dataframe from lower case to upper case with Pandas
<p>I have a dataframe like the one displayed below: </p>

<pre><code># 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'])
</code></pre>

<p><a href="http://i.stack.imgur.com/fiOEt.png" rel="nofollow"><img src="http://i.stack.imgur.com/fiOEt.png" alt="enter image description here"></a></p>

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

<p><a href="http://i.stack.imgur.com/gprF6.png" rel="nofollow"><img src="http://i.stack.imgur.com/gprF6.png" alt="enter image description here"></a></p>

<p>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.</p>

<p>What I tried so far is to do this but without success</p>

<pre><code>df.str.upper()
</code></pre>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.astype.html" rel="nofollow">astype()</a> will cast each series to the <a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#astype" rel="nofollow">dtype</a> object (string) and then call the <a href="http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.str.html#pandas.Series.str" rel="nofollow">str()</a> method on the converted series to get the string literally and call the function <a href="https://docs.python.org/2/library/stdtypes.html#str.upper" rel="nofollow">upper()</a> on it. Note that after this, the dtype of all columns changes to object.</p>

<pre><code>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
</code></pre>

<p>You can later convert the 'battles' column to numeric again, using <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html#pandas.to_numeric" rel="nofollow">to_numeric()</a>:</p>

<pre><code>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
</code></pre>

python pandas type-conversion uppercase lowercase