使用Pandas,根据ID和新值列表更新列值

3

我有一个包含ID和Sell两列的数据框。我想使用新的Sell列表来更新Sell列(不需要更新所有行 - 仅需要更新其中一些)。在我看到的所有示例中,值始终相同或来自某一列。而在我的情况下,我有一个动态值。

这是我想要的:

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)

有什么想法吗? 提前感谢。

2个回答

1
假设“id”是一个字符串(如IDList中所述),并且不是您的数据框的索引。
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是index:
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。

嗨,Gupta。太棒了,它完美地运行了!我将ID转换为int,因为默认情况下它们是str。我使用了两个选项,它们都很好用。非常感谢。只有一个问题,itertuples()在这里不更好吗?我在Pandas的文档中查看了一下,他们推荐使用itertuples()而不是iterrows()。 - crianopa

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接