Converting a nested array into a pandas dataframe in python
<p>I'm attempting to convert several dictionaries contained in an array to a pandas dataframe. The dicts are saved as such: </p>

<pre><code>[[{u'category': u'anti-social-behaviour',u'location': {u'latitude': u'52.309886',
u'longitude': u'0.496902'},u'month': u'2015-01'},{u'category': u'anti-social-behaviour',u'location': {u'latitude': u'52.306209',
u'longitude': u'0.490475'},u'month': u'2015-02'}]]
</code></pre>

<p>I'm trying to format my data to the format below:</p>

<pre><code>     Category      Latitude   Longitude
0    anti-social   524498.597 175181.644
1    anti-social   524498.597 175181.644
2    anti-social   524498.597 175181.644
.    ...           ...
.    ...           ...
.    ...           ...
</code></pre>

<p>I've tried to force the data into a dataframe with the below code but it doesn't produce the intended output.</p>

<pre><code>for i in crimes:
    for x in i:
        print pd.DataFrame([x['category'], x['location']['latitude'], x['location']['longitude']])
</code></pre>

<p>I'm very new to Python so any links/tips to help me build this dataframe would be highly appreciated!</p>

<p>You are on the right track, but you are creating a new dataframe for each row and not giving the proper <code>columns</code>. The following snippet should work:</p>

<pre><code>import pandas as pd
import numpy as np

crimes = [[{u'category': u'anti-social-behaviour',u'location': {u'latitude': u'52.309886',
u'longitude': u'0.496902'},u'month': u'2015-01'},{u'category': u'anti-social-behaviour',u'location': {u'latitude': u'52.306209',
u'longitude': u'0.490475'},u'month': u'2015-02'}]]

# format into a flat list
formatted_crimes = [[x['category'], x['location']['latitude'], x['location']['longitude']] for i in crimes for x in i]

# now pass the formatted list to DataFrame and label the columns
df = pd.DataFrame(formatted_crimes, columns=['Category', 'Latitude', 'Longitude'])
</code></pre>

<p>The result is:</p>

<pre><code>                Category   Latitude Longitude
0  anti-social-behaviour  52.309886  0.496902
1  anti-social-behaviour  52.306209  0.490475
</code></pre>

python python-2.7