Skip to content Skip to sidebar Skip to footer

Separate Pd Dataframe Rows That Are Dictionaries Into Columns

I am extracting some data from an API and having challenges transforming it into a proper dataframe. The resulting DataFrame df is arranged as such: Index Column 0 {'email

Solution 1:

One naive way of doing this is as below:

inp = [{'email@email.com': [{'action': 'data', 'date': 'date'}, {'action': 'data', 'date': 'date'}]}
 , {'different-email@email.com': [{'action': 'data', 'date': 'date'}]}]
index = 0
df = pd.DataFrame()
foreach in inp: # iterate through the list of dictsfor k, v in each.items(): #take each key value pairsfor eachv in v: #the values being a list, iterate through eachprint (str(eachv))
            df.set_value(index,'Column1',k)
            df.set_value(index,'Column2',str(eachv))
            index += 1

I am sure there might be a better way of writing this. Hope this helps :)

Solution 2:

Assuming you have already read it as dataframe, you can use following -

import ast
df['Column'] = df['Column'].apply(lambda x: ast.literal_eval(x))
df['email'] = df['Column'].apply(lambda x: x.keys()[0])
df['value'] = df['Column'].apply(lambda x: x.values()[0])

Post a Comment for "Separate Pd Dataframe Rows That Are Dictionaries Into Columns"