I have the following dataframe in Pandas:
column_names = ["item", "item_dict", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7", "loc8", "loc9"]
data = [['p26CE0DEAC1', {'loc1': 50, 'loc4': 50, 'loc7': 33}, 0, 0, 0, 0, 0, 0, 0, 0, 0], ['p5505CB1A96', {'loc1': 48}, 0, 0, 0, 0, 0, 0, 0, 0, 0], ['p1B9E6A73EC', {}, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
df_sample = pd.DataFrame(data, columns = column_names)
The first column ["item"] is an identification number of an item. The second column ["item_dict"] is a dictionary with a key showing an item location and a value showing the quantity of the item at the location. The third through eleventh columns correspond to each of the locations where items are stored and those columns labels match the keys from the item_dict column.
What I'm trying to figure out is how to take the dictionary embedded in each row's item_dict column and move the values to the columns with a name that matches the key in that dictionary.
I want the output to end up being:
column_names = ["item", "item_dict", "loc1", "loc2", "loc3", "loc4", "loc5", "loc6", "loc7", "loc8", "loc9"]
data = [['p26CE0DEAC1', {'loc1': 50, 'loc4': 50, 'loc7': 33}, 50, 0, 0, 50, 0, 0, 33, 0, 0], ['p5505CB1A96', {'loc1': 48}, 48, 0, 0, 0, 0, 0, 0, 0, 0], ['p1B9E6A73EC', {}, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
df_sample = pd.DataFrame(data, columns = column_names)
As a hobbyist I'm not sure how to do this. Each of my attempts have been horrible failures. If someone even has a reference for using dictionaries that are embedded in a pandas dataframe they can point me to, I'd appreciate it but I wasn't able to find anything on stack overflow addressing this scenario.