为每一行从字典中添加新列到dataframe



我想从随机生成键的字典中添加数据帧中的列对于数据框的每一行,应该添加键值

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   

我该怎么做呢?

expecetd结果:

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

您可以从字典中创建一个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')
)

我猜如果你使用的是panda,你可以试着读取字典中的键和值,然后为每个键添加5倍的对应值:

for key, value in products :
new_column_value = []
for i in range(5):
new_column_value.append(value)
df[key] = new_column_value

最新更新