2

I have a dataframe in pandas with two columns. One is an ID and the other is a long JSON object, which is the same object for each object in the dataframe. My goal here is to create columns for each key in the JSON object.

Here is an example of the input

ID  request_json
175431467   {"Rate":"50","Groups":"7 months - 3 years"

I'd like to expand this into a dataframe with three columns: ID, Rate, and Groups.

What's the best way to do this?

2 Answers 2

4

You can use DataFrame constructor with join or concat:

import json

df = df[['ID']].join(pd.DataFrame(df['request_json'].apply(json.loads).values.tolist()))
print (df)
          ID              Groups Rate
0  175431467  7 months - 3 years   50

Or:

df = pd.concat([df['ID'],
                pd.DataFrame(df['request_json'].apply(json.loads).values.tolist())], axis=1)
print (df)
          ID              Groups Rate
0  175431467  7 months - 3 years   50
Sign up to request clarification or add additional context in comments.

1 Comment

Neither of these are working. For both, it just renames the request_json column to 0.
4
In [38]: pd.io.json.json_normalize(df.to_dict('r'))
Out[38]:
          ID request_json.Groups request_json.Rate
0  175431467  7 months - 3 years                50

2 Comments

This doesn't change my dataframe
@mangodreamz, you would need to assign it back or to another variable

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.