我有一个pandas数据帧,我在其中存储了mongoDB的objectID。我想从mongoDB中获取新文档,为此我使用的是存储在pandas dataframe中的最后一个文档ObjectId。但是当我查询数据库时,它给了我一个错误。
这是我正在做的事情。
df = pd.DataFrame(list(db.dataset2.find()))
last_inserted_id = (df['_id'].tail(1))
for i in db.dataset2.find({"_id": {"$gt": ObjectId(last_inserted_id) }}):
print i
然后它给了我一个错误..
TypeError: id must be an instance of (str, unicode, ObjectId), not <class
'pandas.core.series.Series'>
这就是我的数据框架的样子..
_id \
0 5663110c691db01e0cac9d89
1 56631205691db01e0cac9d8a
dish order_area order_id
0 [{u'dish_substitute': u'Yes', u'home_chef_name... NaN order_1
1 [{u'dish_substitute': u'Yes', u'home_chef_name... NaN order_2
order_lat order_long order_time user_id
0 73.955741 40.772027 2015-12-05 16:30:04.345 user_1
1 43.955741 70.772027 2015-12-05 16:34:13.281 user_2
如何处理?
请帮忙..
答案 0 :(得分:1)
print df
# _id no
#0 5663110c691db01e0cac9d89 55
#1 56631205691db01e0cac9d8a 77
#convert to string approach
last_inserted_id = "".join(df['_id'].astype('str').tail(1).tolist())
print last_inserted_id
#56631205691db01e0cac9d8a
#tail only approach
last_inserted_id = "".join(df['_id'].tail(1))
print last_inserted_id
#56631205691db01e0cac9d8a
#iloc approach
last_inserted_id_iloc = df['_id'].iloc[-1]
print last_inserted_id_iloc
#56631205691db01e0cac9d8a