UnicodeDecodeError Help: Data needs to be in ascii for seaborn but source code is in utf-8
<p>Context: I'm trying to build a seaborn heatmap in order to map the following type of data (in a dataframe):</p>

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

<p>(This can be up to 50 fruits and 5500 stores)</p>

<p>My problem (I think) is that seaborn appears to want to use ascii but my data is in utf-8. When I read the csv file, I can't do the following:</p>

<pre><code>df = pd.read_csv('data.csv', encoding = 'ascii')
</code></pre>

<p>without getting the following error:</p>

<pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: ordinal not in range(128)
</code></pre>

<p>When I bring it in using utf-8, it will read and I can reshape it to heatmap-friendly form but then when trying to run:</p>

<pre><code>sns.heatmap(df2)
</code></pre>

<p>I get a similar UnicodeDecodeError</p>

<p>I do have simple special characters (colons, backspaces, etc.) in either my store or fruit fields so I'm wondering what the best approach is here.  </p>

<ul>
<li>Should I run something on my dataframe to remove the utf-8 character
then encode in ascii?   </li>
<li>Should I be doing something to my source .csv file to remove the utf-8 characters?</li>
<li>Can I run seaborn another way to let is accept the encoding I have?</li>
</ul>

<p>If anybody has a preferred method, can they help me with the proper code to get it done?</p>

<p>Python version 2.7.12 :: Anaconda 4.1.1 (64-bit)
Pandas (0.18.1)
Seaborn (0.7.1)</p>

<p>Your configuration (Python 2.7, Pandas 0.18.1, Seaborn 0.7.1) should certainly be able to handle utf-8. Even if the font used in the plot doesn't support these unicode characters, the heatmap should still be displayed. Here is a test case:</p>

<pre class="lang-py prettyprint-override"><code>import pandas as pd
import seaborn as sns

df = pd.DataFrame(
        {'Fruit': ['Apple', 'Banana', 'Orange', 'Kiwi'] * 2,
        'Store': [u'Kr\xf6ger'] * 4 + [u'F\u0254\u0254d Li\u01ebn'] * 4,
        'Stock': [6, 1, 3, 4, 1, 7, 7, 9]})

sns.heatmap(df.pivot("Fruit", "Store", "Stock"))
</code></pre>

<p>The problem, therefore, is somewhere in your data frame <code>df2</code>. Your comment states that <code>df2</code> is created by reshaping another data frame, probably also by something like <code>pivot()</code> or <code>crosstab()</code>. </p>

<p>Let's assume that this original data frame contains the columns <code>Store</code> and <code>Fruit</code>, and that it was read from  your file like so, i.e. with default encoding:</p>

<pre><code>raw = pd.read_csv('data.csv')
</code></pre>

<p>For testing, this is the content of that file <code>data.csv</code>:</p>

<pre class="lang-none prettyprint-override"><code>Store,Fruit,Stock
KrÃ¶ger,Apple,6
KrÃ¶ger,Banana,1
KrÃ¶ger,Orange,3
KrÃ¶ger,Kiwi,4
FÉÉd LiÇ«n,Apple,1
FÉÉd LiÇ«n,Banana,7
FÉÉd LiÇ«n,Orange,7
FÉÉd LiÇ«n,Kiwi,9
</code></pre>

<p>Now, in order to fix the encoding of columns <code>Store</code> and <code>Fruit</code> so that they contain valid Unicode strings, use the <a href="https://docs.python.org/2/library/stdtypes.html#str.decode" rel="nofollow"><code>decode()</code></a> string method, like so:</p>

<pre><code>raw["Store"] = raw["Store"].apply(lambda x: x.decode("utf-8"))
raw["Fruit"] = raw["Fruit"].apply(lambda x: x.decode("utf-8"))
</code></pre>

<p>Now, <code>heatmap()</code> should work happily with the data frame:</p>

<pre><code>sns.heatmap(raw.pivot("Fruit", "Store", "Stock"))
</code></pre>

python pandas unicode encoding seaborn