I have a column of dictionaries like this:
id element
1 {'Colour': 'Grey', 'Brand': 'AB'}
2 {'Colour': 'Blue', 'Brand': 'B'}
3 {'Colour': 'Red', 'Brand': 'AH'}
And I want to create new columns from those dictionaries, like this:
id element colour brand
1 {'Colour': 'Grey', 'Brand': 'AB'} Grey AB
2 {'Colour': 'Blue', 'Brand': 'B'} Blue B
3 {'Colour': 'Red', 'Brand': 'AH'} Red AH
I have done the following but it's not working:
def whatever(row):
tmp_d = {}
for d in row.values:
for k in d.keys():
if k in tmp_d.keys():
tmp_d[k] += 1
else:
tmp_d[k] = 1
return tmp_d
new_df.colour = df.groupby('element')'element'].apply(whatever).unstack().fillna(0)
Data:
data = {'id': [1, 2, 3],
'element': ["{'Colour': 'Grey', 'Brand': 'AB'}",
"{'Colour': 'Blue', 'Brand': 'B'}",
"{'Colour': 'Red', 'Brand': 'AH'}"]}