我想从字典中添加列,在字典中随机生成键,对于数据的每一行,都应该添加键的值。
products = {'Blue Racks': 6, 'HPT': 6, 'Plastic Pallet': 40, 'Trolley': 48}数据格式如下所示:
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 我该怎么做呢?
预期结果:
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发布于 2022-05-23 11:37:39
您可以从字典中创建一个DataFrame,并使用交叉merge。
df2 = df.merge(pd.DataFrame(products, index=[0]), how='cross')
# or
# df.merge(pd.DataFrame([products]), how='cross')产出:
tag epc code Location Blue Racks HPT \
0 0 E200001B2003006506902124 PPRFP.1T22AD0001 Goa 6 6
1 1 E200001B2001007523803291 PPRFP.1T22AD0002 Goa 6 6
2 2 E200001B2003005907402139 PPRFP.1T22AD0003 Goa 6 6
3 3 E200001B200302290510CF16 PPRFP.1T22AD0004 Goa 6 6
4 4 E200001B20010114231054DD PPRFP.1T22AD0005 Goa 6 6
Plastic Pallet Trolley
0 40 48
1 40 48
2 40 48
3 40 48
4 40 48 如果字典中存在原始列,则重新命名:
df2 = (df.rename(columns=lambda x: x+'_original' if x in products else x)
.merge(pd.DataFrame(products, index=[0]), how='cross')
)发布于 2022-05-23 11:40:49
我想,如果您使用熊猫,您可以尝试读取字典中的键和值,然后为每个键添加5倍于相应值的值:
for key, value in products :
new_column_value = []
for i in range(5):
new_column_value.append(value)
df[key] = new_column_valuehttps://stackoverflow.com/questions/72347807
复制相似问题