How To Unpack Multiple Dictionary Objects Inside List Within A Row Of Dataframe?
I have a dataframe with the below dictionaries within a single list in every row and per row, the list are different sizes with they are of different sizes as below: ID unnest_c
Solution 1:
IIUC, from
df = pd.DataFrame()
df['x'] = [[{'QuestionId': 11, 'ResponseId': 1},{'QuestionId': 15, 'ResponseId': 1},
{'QuestionId': 16, 'ResponseId': 1},
{'QuestionId': 17, 'ResponseId': 1},
{'QuestionId': 18, 'ResponseId': 1, 'Value': 'abc'},
{'QuestionId': 23, 'DataLabel': 'xxx', 'ResponseId': 1},
{'QuestionId': 23, 'DataLabel': 'xxx', 'ResponseId': 2},
{'QuestionId': 23, 'DataLabel': 'xxx', 'ResponseId': 4}],
[{'QuestionId': 11, 'ResponseId': 1}]]
You can sum
your lists to aggregate them, and use DataFrame
constructor
new_df = pd.DataFrame(df.x.values.sum())
DataLabel QuestionId ResponseId Value
0NaN111NaN1NaN151NaN2NaN161NaN3NaN171NaN4NaN181 abc
5 xxx 231NaN6 xxx 232NaN7 xxx 234NaN8NaN111NaN
If you want to maintain the original indexes, you can build a inds
list and pass it as arguments to the constructor:
inds =[index for _ in([i]* len(v)for i,v in df.x.iteritems())for index in _]
pd.DataFrame(df.x.values.sum(), index=inds)
DataLabel QuestionId ResponseId Value
0NaN111NaN0NaN151NaN0NaN161NaN0NaN171NaN0NaN181 abc
0 xxx 231NaN0 xxx 232NaN0 xxx 234NaN1NaN111NaN
Post a Comment for "How To Unpack Multiple Dictionary Objects Inside List Within A Row Of Dataframe?"