pandas json_normalize with very nested json

In the pandas example (below) what do the brackets mean? Is there a logic to be followed to go deeper with the []. […] result = json_normalize(data, ‘counties’, [‘state’, ‘shortname’, [‘info’, ‘governor’]]) Each string or list of strings in the [‘state’, ‘shortname’, [‘info’, ‘governor’]] value is a path to an element to include, in addition … Read more

How to read a JSON file with nested objects as a pandas DataFrame?

You can use json_normalize: import json with open(‘myJson.json’) as data_file: data = json.load(data_file) df = pd.json_normalize(data, ‘locations’, [‘date’, ‘number’, ‘name’], record_prefix=’locations_’) print (df) locations_arrTime locations_arrTimeDiffMin locations_depTime \ 0 06:32 1 06:37 1 06:40 2 08:24 1 locations_depTimeDiffMin locations_name locations_platform \ 0 0 Spital am Pyhrn Bahnhof 2 1 0 Windischgarsten Bahnhof 2 2 Linz/Donau Hbf … Read more

How can I convert JSON to CSV?

With the pandas library, this is as easy as using two commands! df = pd.read_json() read_json converts a JSON string to a pandas object (either a series or dataframe). Then: df.to_csv() Which can either return a string or write directly to a csv-file. See the docs for to_csv. Based on the verbosity of previous answers, … Read more

Split / Explode a column of dictionaries into separate columns with pandas

To convert the string to an actual dict, you can do df[‘Pollutant Levels’].map(eval). Afterwards, the solution below can be used to convert the dict to different columns. Using a small example, you can use .apply(pd.Series): In [2]: df = pd.DataFrame({‘a’:[1,2,3], ‘b’:[{‘c’:1}, {‘d’:3}, {‘c’:5, ‘d’:6}]}) In [3]: df Out[3]: a b 0 1 {u’c’: 1} 1 … Read more