I export a Postgres SQL query to create the following Pandas data frame df
:
df= pd.DataFrame({
'employee_id' : [123, 456, 789],
'sales' : [{'foo':2, 'bar': 0, 'baz': 1},
{'foo':3, 'bar': 1, 'baz': 2},
{'foo':7, 'bar': 0, 'baz': 4}]
})
df
employee_id sales
0 123 {'foo': 2, 'bar': 0, 'baz': 1}
1 456 {'foo': 3, 'bar': 1, 'baz': 2}
2 789 {'foo': 7, 'bar': 0, 'baz': 4}
The data types are:
df.dtypes
employee_id int64
sales object
dtype: object
I would like to split the sales
column of dictionaries into separate columns by the keys, as follows:
employee_id foo bar baz
0 123 2 0 1
1 456 3 1 2
2 789 7 0 4
Following the advise in Split / Explode a column of dictionaries into separate columns with pandas, I do so using the following:
df2 = pd.json_normalize(df['sales'])
Which gives:
df2.head()
0
1
2
3
4
When I apply this approach to non-production data, it works.
How do I split the sales
column into 3 separate columns?
Thanks!