Hi I have the following pandas data frame:
df = pd.DataFrame({'info':[1.4,3.6,6.5], 'new':[[{'score':0.998, 'letters':'C', 'temp':1}, {'score':1.343, 'letters':'B', 'temp':0}, {'score':2.323, 'letters':'F', 'temp':1}], [{'score':2.532, 'letters':'D', 'temp':1}, {'score':2.123, 'letters':'G', 'temp':1}, {'score':4.332, 'letters':'S', 'temp':0}], [{'score':2.223, 'letters':'C', 'temp':0}, {'score':1.144, 'letters':'J', 'temp':1}, {'score':7.443, 'letters':'G', 'temp':9}]]})
df:
info new
0 1.4 [{'score': 0.998, 'letters': 'C', 'temp': 1}, ...
1 3.6 [{'score': 2.532, 'letters': 'D', 'temp': 1}, ...
2 6.5 [{'score': 2.223, 'letters': 'C', 'temp': 0}, ...
I'm trying to get the score values and letters values from each dictionary and get the following output:
info score1 score2 score3 letters1 letters2 letters3
0 1.4 0.998 1.343 2.323 C B F
1 3.6 2.532 2.123 4.332 D G S
2 6.5 2.223 1.144 7.443 C J G
I have tried this:
from pandas.io.json import json_normalize
records = [x[0] for x in df['new']]
res = pd.concat([df.drop('new', axis=1), json_normalize(records)], axis=1)
I have also tried:
df = df['new'].apply(lambda x: pd.Series({i['score']: i['letters'] for i in x if isinstance(x, list)}))
Any help would be great