title: Write or log print output of pandas Dataframe
body: <p>I have a Dataframe I wish to write a few rows of into a file and logger in Python 2.7. <code>print(dataframe.iloc[0:4])</code> outputs a nice grid of the column headers and top 4 rows in the dataframe. However <code>logging.info(dataframe.iloc[0:4])</code> throws:</p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 87: ordinal not in range(128)</p>
  </blockquote>
  
  <p>Here is the output to console, works either directly to console or via <code>print()</code> (note the <code>²</code>):</p>
  
  <pre><code>In[89]: d.iloc[0:4]    OR   print(d.iloc[0:4])
  Out[89]: 
     ISO  ID_0     NAME_0  ID_1                           NAME_1    ID_2    NAME_2  Area(km.²)  Pop2001_Cen  Pop2010_Cen  HHold2010  Hhold_Size
  0  ARG    12  Argentina     2  Ciudad Autónoma de Buenos Aires     NaN       NaN       203.0    2776138.0      2890151  1150134.0    2.512882
  1  ARG    12  Argentina     2  Ciudad Autónoma de Buenos Aires  2001.0  Comuna 1         NaN     171975.0       205886    84468.0    2.437444
  2  ARG    12  Argentina     2  Ciudad Autónoma de Buenos Aires  2002.0  Comuna 2         NaN     165494.0       157932    73156.0    2.158839
  3  ARG    12  Argentina     2  Ciudad Autónoma de Buenos Aires  2003.0  Comuna 3         NaN     184015.0       187537    80489.0    2.329971
  </code></pre>
  
  <p>As does <code>file.write(dataframe.iloc[0:4])</code> and so on, as one of the column headers includes a non-ascii character. I have tried all sorts of variations of <code>decode()</code>, <code>encode()</code>, etc, but cannot avoid this error.</p>
  
  <p><code>print(d.iloc[0:4])</code> works, so another approach was to use <code>print(d.iloc[0:4], file=f)</code> but even with <code>from __future__ import print_function</code> I get the above ascii encoding error.</p>
  
  <p>Other ways to replicate this problem are <code>logging.info('Area(km.²)')</code> or <code>'Area(km.²)'.decode()</code></p>
  
  <p>How can I render this dataframe?</p>
  
  <p>[Edit:]</p>
  
  <p>I also want to understand fundamentally how I deal with string encoding/decoding in Python 2.7 - I've been hacking away at this for more time than it deserves because this isn't the only time I've had this <code>UnicodeDecodeError</code> error, and I don't know when it'll occur and I am still just throwing fixes at the console to see what sticks, without any underlying understanding of what's going on.</p>