【发布时间】:2018-03-09 13:49:20
【问题描述】:
在使用 pandas 进行一些分析后,我正在尝试更新 MongoDB 中的集合,这是我的代码:
client=MongoClient()
db=client.database
cll=db.collection
cursor=cll.find()
df=pd.DataFrame(list(cursor))
df['new_field'] = df['existing_field_A'].apply(lambda x: personalized_function(x))
for index, row in df.iterrows():
_id=row['_id']
new_value=row['new_field']
cll.update_one({'_id':_id}, {'$set':{'new_field':new_value}})
代码运行良好,但需要很长时间。我想知道是否有更好的方法来更新我的收藏。
【问题讨论】:
标签: python mongodb pandas pymongo