Skip to content

Instantly share code, notes, and snippets.

@teh
Created April 9, 2015 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teh/89f93bcc911a3c664789 to your computer and use it in GitHub Desktop.
Save teh/89f93bcc911a3c664789 to your computer and use it in GitHub Desktop.
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