Skip to content
Advertisement

How to extract json from nested column to dataframe

I’m pulling stock data from TD Ameritrade API and I want to store it in a DataFrame.

From the API I get a nested JSON object and when I put it in a data frame I get 4 columns: Index, Candles, Empty, Symbol. However inside of candles is a dictionary that I want as separate columns in the dataframe (‘open’,’close’,…)

I’ve tried json_normalize and pd.io.json.json_normalize

neither gave me the desired result

JavaScript

Output:

JavaScript

….

JavaScript

Input:

pd.DataFrame(data)

Output:

Data frame with 4 columns (‘Index’, ‘Candles’, ‘Empty’, ‘Symbol’). The Candles column is a dictionary. I’m trying to get all the keys in the dictionaries as columns and the key values as rows in the dataframe

Advertisement

Answer

You’re using json_normalize 1 level too high. You’re wanting to normalize/flatten out the data under data['candles']:

I’d also be careful about posting api keys.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement