1

So I have the following dataframe:

enter image description here

The JSON blobs all look something like this:

{"id":"dddd1", "random_number":"77777"}

What I want my dataframe to look like is something like this:

enter image description here

Basically what I need is to get a way to iterate and normalize all the JSON blob columns and put them back in the dataframe in the proper rows (0-99). I have tried the following:

pd.json_normalize(data_frame.iloc[:, JSON_0,JSON_99])

I get the following error:

IndexingError: Too many indexers

I could go through and normalize each JSON_BLOB column individually however that is inefficient, I cant think of a proper way to do this via a Lambda function or for loop because of the JSON blob. The for loop I wrote gives me the same error:

array=[]
for app in data_frame.iloc[:, JSON_0,JSON_99]:
    data = {
        'id': data['id']
        
    }
    array.append(data)


test= pd.DataFrame(array)


IndexingError: Too many indexers

Also some of the JSON_Blobs have NAN values

Any suggestions would be great.

1
  • I have a funny feeling this is gonna be super easy when I read the responses btw Commented Dec 2, 2022 at 20:02

1 Answer 1

2

Can you try this:

normalized = pd.concat([df[i].apply(pd.Series) for i in df.iloc[:,2:]],axis=1) #2 is the position number of JSON_0.
final = pd.concat([df[['Root_id_PK','random_number']],normalized],axis=1)

if you want the column names as in the question:

normalized = pd.concat([df[i].apply(pd.Series).rename(columns={'id':'id_from_{}'.format(i),'random_number':'random_number_from_{}'.format(i)}) for i in df.iloc[:,2:]],axis=1)
final = pd.concat([df[['Root_id_PK','random_number']],normalized],axis=1)
Sign up to request clarification or add additional context in comments.

5 Comments

it looks like it works but my notebook cant handle this much data, Im gonna try to cut down on the cells and see if it works
Ok. I made an update to the answer, Check it again.
wait, I am preparing a much more effective solution.
Sorry, the method i tried took a little longer than the answer. So the solution in my answer is faster.
TY, I was on this one for a while, much apprecaited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.