我有一个带有ID和Sell列的df。我想使用新的卖单列表来更新“卖出”列(并非所有原始商品都需要更新-只是其中一些)。在我看到的所有示例中,该值始终相同或来自一列。就我而言,我具有动态价值。
这就是我想要的:
file = ('something.csv') # Has 300 rows
IDList= [['453164259','453106168','453163869','453164463'] # [ID]
SellList=[120,270,350,410] # Sells values
csv = path_pattern = os.path.join(os.getcwd(), file)
df = pd.read_csv(file)
df.loc[df['Id'].isin(IDList[x]), 'Sell'] = SellList[x] # Update the rows with the corresponding Sell value of the ID.
df.to_csv(file)
有什么想法吗? 预先感谢
答案 0 :(得分:1)
假设“ id”是一个字符串(如IDList中所述)且不是df的索引
IDList= [['453164259','453106168','453163869','453164463'] # [ID]
SellList=[120,270,350,410]
id_dict={x:y for x,y in zip(IDList,SellList)}
for index,row in df.iterrows():
if row['id'] in IDList:
df.loc[str(index),'Sell']=id_dict[row['id']]
如果id为索引:
IDList= [['453164259','453106168','453163869','453164463'] # [ID]
SellList=[120,270,350,410]
id_dict={x:y for x,y in zip(IDList,SellList)}
for index,row in df.iterrows():
if index in IDList:
df.loc[str(index),'Sell']=id_dict[index]
我所做的是使用IDlist和SellList创建了一个字典,然后使用iterrows()在df上循环了
答案 1 :(得分:1)
df = pd.read_csv('something.csv')
IDList= ['453164259','453106168','453163869','453164463']
SellList=[120,270,350,410]
这将有效地工作,特别是对于大文件:
df.set_index('id', inplace=True) df.loc[IDList, 'Sell'] = SellList df.reset_index() ## not mandatory, just in case you need 'id' back as a column
df.to_csv(file)