'Add new columns to dataframe from dictionary for every row
I want to add columns in a dataframe from a dictionary where keys are randomly generated and for every row of the dataframe the values of keys should be added
products = {'Blue Racks': 6, 'HPT': 6, 'Plastic Pallet': 40, 'Trolley': 48}
and the dataframe is like following:
tag epc code Location
0 0 E200001B2003006506902124 PPRFP.1T22AD0001 Goa
1 1 E200001B2001007523803291 PPRFP.1T22AD0002 Goa
2 2 E200001B2003005907402139 PPRFP.1T22AD0003 Goa
3 3 E200001B200302290510CF16 PPRFP.1T22AD0004 Goa
4 4 E200001B20010114231054DD PPRFP.1T22AD0005 Goa
How can I do it ?
expecetd outcome:
tag epc code Location Blue Racks HPT Plastic Pallet
E200001B2003006506902124 PPRFP.1T22AD0001 Goa 6 6 40
E200001B2001007523803291 PPRFP.1T22AD0002 Goa 6 6 40
E200001B2003005907402139 PPRFP.1T22AD0003 Goa 6 6 40
E200001B200302290510CF16 PPRFP.1T22AD0004 Goa 6 6 40
E200001B20010114231054DD PPRFP.1T22AD0005 Goa 6 6 40
Solution 1:[1]
I guess if you're using panda, you could try and read the key and values of your dictionnary, then add for each key 5 times the corresponding value :
for key, value in products :
new_column_value = []
for i in range(5):
new_column_value.append(value)
df[key] = new_column_value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maxime Bouhadana |
