Writing pandas DataFrame to JSON in unicode
<p>I'm trying to write a pandas DataFrame containing unicode to json, but the built in <code>.to_json</code> function escapes the characters. How do I fix this?</p>

<p>Some sample code:</p>

<pre><code>import pandas as pd
df=pd.DataFrame([['Ï','a',1],['Ï','b',2]])
df.to_json('df.json')
</code></pre>

<p>gives:</p>

<pre><code>{"0":{"0":"\u03c4","1":"\u03c0"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
</code></pre>

<p>instead of what I want:</p>

<pre><code>{"0":{"0":"Ï","1":"Ï"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
</code></pre>

<p>Adding the <code>force_asciii=False</code> argument gives me the following error:
<code>UnicodeEncodeError: 'charmap' codec can't encode character '\u03c4' in position 11: character maps to &lt;undefined&gt;</code></p>

<p>I'm using WinPython 3.4.4.2 64bit with pandas 0.18.0</p>

<p>Opening a file with the encoding set to utf-8, and then passing that file to the <code>.to_json</code> function fixes the problem:</p>

<pre><code>with open('df.json', 'w', encoding='utf-8') as file:
    df.to_json(file, force_ascii=False)
</code></pre>

<p>gives the correct:</p>

<pre><code>{"0":{"0":"Ï","1":"Ï"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
</code></pre>

<p>Note: it does still require the <code>force_ascii=False</code> argument.</p>

python json pandas unicode