任务是这样的:在文件 1.csv 中有一个具有“OR-SDR-28HD-OLEC”类型的唯一值的列。此列中的每个单元格对应于相邻列中单元格的值。有一个 2.csv 文件,它的值来自 1.csv 文件的第一列。2.csv文件中第一列的值需要从第二列中拉出对应的值。
1.csv 看起来像这样:
Overwrite=No,,"* Changing the setting to ?Overwrite=Yes? will have the added effect of deactivating all of your items from the website except for those listed on this datafeed. If this is not intended, keep ?Overwrite=No?.",,,,,,,,
Part #,Item #,Currency,MSRP,MAP,Checkout MAP,Selling Price,Inventory,Fulfillment Option,Shipping,Activation Mark
PS2-BEC-5780,9SIAFDG6V86915,USD,,0.00,False,26.98,37,Seller,free,True
2.csv 应在第二列中包含来自 1.csv 的项目编号
Overwrite = Yes,,* Changing the setting to ?Overwrite=Yes? will have the added effect of deactivating all of your items from the website except for those listed on this datafeed. If this is not intended, keep ?Overwrite=No?.
Part #,Item #,Currency,MSRP,MAP,Checkout MAP,Selling Price,Inventory,Fulfillment Option,Shipping,Activation Mark
PS2-BEC-5780,,USD,,0.00,False,26.98,37,Seller,free,True
唯一值可以用作索引
pandas.DataFrame,然后您可以简单地使用以下方法获得所需的值df.loc[]:输出示例:
如果我正确理解了任务:
如果您只想从另一个 CSV 文件(DataFrame)添加一列,那么使用该方法会更有效
.map():例子:
更新: pandas.DataFrame 是内存中的“表”结构。要将 DataFrame 保存为 CSV,可以使用DataFrame.to_csv()方法