1

Read using Pandas Method 1 : I've read the json file using pandas and i've a dataframe in my hand, but as you can see in the pic(link)there are certain columns where the element itself a dict, how do i create columns in the current dataframe with the keys of the dict as column with respective values.

Read using 'with open' Method 2 : I've attached another image, in this i've read the file using 'with open', so it'll be a list and then converted to pandas dataframe using json.normalize(). When i use this i can easily obtain what i want.

So, how do i convert my dataframe with new columns while using method 1 ? Look at the 'Type' column in both images to understand what i mean ? PS : I didn't have enough badges to post the pic

Thanks for answering in advance !

Guna
  • 25
  • 7
  • 1
    dont post pics. post data and expected output [guide](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – sammywemmy May 18 '20 at 11:54

1 Answers1

0

First, you have a df like this : enter image description here

I think you should make a def() for the 'key' column as is:

def item_x (row, column, key) :
    x = row[column][key]
    return x

Then, you can create new columns, let's say 'id_type' and 'name_type' (for the 'type' column of your dataframe:

# create new column with the dict values
df['id_type'] = df.apply (lambda row: item_x (row, 'type', 'id'),axis=1)
df['name_type'] = df.apply (lambda row: item_x (row, 'type', 'name'),axis=1)

and finally, drop the 'type' column if needed :

df = df.drop('type', axis=1)

Finally you have : enter image description here

Joke
  • 30
  • 2