1

I have a data frame with a JSON string in a column:

ID Data
11 {'Name': 'Sam', 'Age': 21}
22 {'Name': 'Nam', 'Age': 22}
33 {'Name': 'Pam', 'Age': 21, 'Salary': 10000}

How can I convert the above JSON string in to columns?

Desired result:

ID Name Age Salary
11 Sam 21
22 Nam 22
33 Pam 21 10000
4
  • You want to convert to dataframe without pandas? Commented Jul 12, 2022 at 4:47
  • I am using pandas Commented Jul 12, 2022 at 4:48
  • so it must be present in a string format, right? not in json format Commented Jul 12, 2022 at 4:55
  • Yes, its string type but JSON format. @HimanshuPOddar Commented Jul 12, 2022 at 4:59

1 Answer 1

6

You can use pandas.Series to read your column of dictionaries values into columns.

Creating the data

data = {
    'Id' : [11, 22, 33],
    'Data': ["{'Name': 'Sam', 'Age': 21}", "{'Name': 'Nam', 'Age': 22}", "{'Name': 'Pam', 'Age': 21, 'Salary': 10000}"],
    }
df = pd.DataFrame(data)

Converting dictionary to column

df['Data'] = df['Data'].map(lambda x: eval(x) if pd.notnull(x) else x)
df = pd.concat([df, df.pop("Data").apply(pd.Series)], axis=1)

Output :

   Id Name  Age   Salary
0  11  Sam   21      NaN
1  22  Nam   22      NaN
2  33  Pam   21  10000.0

Alternate solution

You can also use json_normalize to unravel the dictionary column to column from dictionary keys.

df['Data'] = df['Data'].map(lambda x: eval(x) if pd.notnull(x) else x)
df = pd.concat([df, pd.json_normalize(df.pop("Data"))], axis=1)

which gives you same output

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way we can get the column list out of it which are added by the json string? col = {'Name','Age','Salary'}
yes, we can use df.pop("Data").apply(pd.Series).columns this gives you ['Name', 'Age', 'Salary'] there are various other ways of doing this

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.