Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split JSON row data into multiple columns in Python

I'm trying to find a way to split (flatten) JSON row data into multiple columns in pandas.

I have a dataframe which looks like the following:

Current Dataframe

This is an example of what a row looks like:

Row example

I'm able to use the json_normalize function on a single row to achieve the following: (It's just shortened as an example)

Code Example

Table

However, when trying to apply the normalize function to the whole dataframe, I get 'str' object has no attribute 'values'

Are there any suggestions for how to go about doing this? Thanks

I apologise for the use of images, but I kept getting a message saying code is not formatted properly

like image 537
wm10 Avatar asked Sep 16 '25 01:09

wm10


2 Answers

If it's a flat json then you can try :-

new_df = pd.DataFrame(df['tickers'].tolist())

The Dataframe constructor takes in a list of dictionary objects and turns the key into columns as default orientation, this is the simplest way if your data is standardized and doesn't have a complex nested structure.

like image 65
fibonachoceres Avatar answered Sep 17 '25 14:09

fibonachoceres


all_data_jsons = df['tickers'].to_list()
df = pd.DataFrame(all_data_jsons)

Maybe you can try this once

like image 44
Ujjwal Agrawal Avatar answered Sep 17 '25 16:09

Ujjwal Agrawal