Python Dataframe column is list of dicts and how parse it
Question:
I have dataframe:
id
name
describe
1
some
[{‘id’:20, ‘name’:’thisIwantAsNameColumn’,’value’:’thisIwantasValueinRow’},{‘id’:22, ‘name’:’thisIwantAsNameColumn2′,’value’:’thisIwantasValueinRow2′}]
2
some2
[{‘id’:23, ‘name’:’thisIwantAsNameColumn’,’value’:’thisIwantasValueinRow’},{‘id’:24, ‘name’:’thisIwantAsNameColumn2′,’value’:’thisIwantasValueinRow2′}]
and i want:
id
name
thisIwantAsNameColumn
thisIwantAsNameColumn2
1
some
thisIwantasValueinRow
thisIwantasValueinRow2
2
some2
thisIwantasValueinRow
thisIwantasValueinRow2
i try write function, but it creates a new dataframe for me and I would then have to connect it through something and that doesn’t work well:
def proccess_customFields(row):
customF={}
for item in row:
customF["custom_field-{}".format(item.get('name'))] = item.get('value')
result= pd.DataFrame.from_dict(customF,orient='index').T
return result
Answers:
If you have a list of dict (and not a JSON string), you can try:
df1 = df.pop('describe').apply(lambda x: pd.Series({l.get('name'): l.get('value') for l in x}))
out = pd.concat([df, df1], axis=1)
print(out)
# Output
id name thisIwantAsNameColumn thisIwantAsNameColumn2
0 1 some thisIwantasValueinRow thisIwantasValueinRow2
1 2 some2 thisIwantasValueinRow thisIwantasValueinRow2
I have dataframe:
id | name | describe |
---|---|---|
1 | some | [{‘id’:20, ‘name’:’thisIwantAsNameColumn’,’value’:’thisIwantasValueinRow’},{‘id’:22, ‘name’:’thisIwantAsNameColumn2′,’value’:’thisIwantasValueinRow2′}] |
2 | some2 | [{‘id’:23, ‘name’:’thisIwantAsNameColumn’,’value’:’thisIwantasValueinRow’},{‘id’:24, ‘name’:’thisIwantAsNameColumn2′,’value’:’thisIwantasValueinRow2′}] |
and i want:
id | name | thisIwantAsNameColumn | thisIwantAsNameColumn2 |
---|---|---|---|
1 | some | thisIwantasValueinRow | thisIwantasValueinRow2 |
2 | some2 | thisIwantasValueinRow | thisIwantasValueinRow2 |
i try write function, but it creates a new dataframe for me and I would then have to connect it through something and that doesn’t work well:
def proccess_customFields(row):
customF={}
for item in row:
customF["custom_field-{}".format(item.get('name'))] = item.get('value')
result= pd.DataFrame.from_dict(customF,orient='index').T
return result
If you have a list of dict (and not a JSON string), you can try:
df1 = df.pop('describe').apply(lambda x: pd.Series({l.get('name'): l.get('value') for l in x}))
out = pd.concat([df, df1], axis=1)
print(out)
# Output
id name thisIwantAsNameColumn thisIwantAsNameColumn2
0 1 some thisIwantasValueinRow thisIwantasValueinRow2
1 2 some2 thisIwantasValueinRow thisIwantasValueinRow2