Skip to content

Instantly share code, notes, and snippets.

@teh

teh/json flatten.py

Created Apr 9, 2015
Embed
What would you like to do?
def get_records():
df = pandas.read_json('1.json')
df = pandas.io.json.json_normalize(df['accidents'])
for _, row in df.iterrows():
for c in row['casualties']:
for i in row['involved']:
row['caualty_severity'] = c['severity']
row['caualty_gender'] = c['gender']
row['caualty_class'] = c['class']
row['caualty_vehicle'] = c.get('vehicle', 'unknonwn')
row['involved_id'] = i['id']
row['involved_type'] = i['type']
yield row
df = pandas.DataFrame.from_records(get_records())
del df['casualties']
del df['involved']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment