Skip to main content
 首页 » 编程设计

python之如何将 Pandas 系列元素转换为字符串

2025年05月04日14qlqwjy

我有一个 pandas 数据框,其中存储了 mongoDB 的 objectID。我想从 mongoDB 中获取新文档,为此我使用存储在 pandas 数据框中的最后一个文档 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 

如何处理?
请帮助..

请您参考如下方法:

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