flattening nested Json in pandas data frame

If you are looking for a more general way to unfold multiple hierarchies from a json you can use recursion and list comprehension to reshape your data. One alternative is presented below: def flatten_json(nested_json, exclude=[”]): “””Flatten json object with nested keys into a single level. Args: nested_json: A nested json object. exclude: Keys to exclude … 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