作者:该改改小心眼_222 | 来源:互联网 | 2022-12-24 18:56
我正在尝试将数据框插入MongoDB。每行应为一个文档。
from pymongo import MongoClient
import pandas as pd
client = MongoClient()
col = client['test']['test']
d = {'name': ['Braund', 'Cummings', 'Heikkinen', 'Allen'],
'age': [22,38,26,35],
'fare': [7.25, 71.83, 0 , 8.05],
'survived?': [False, True, True, False]}
df = pd.DataFrame(d)
col.insert_many(df)
但是,以上代码返回错误: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
更改col.insert_many(df)
到col.insert_many(df.to_dict())
或col.insert_many(df.to_json())
原因TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
1> 小智..:
您非常亲密,我们可以重用您的代码。
注意,我们仍然使用pymongo.MongoClient
和pandas.DataFrame.to_dict
,并且在后者中添加了一个参数。
to_dict(orient='records')
然后,我们将有
from pymongo import MongoClient
import pandas as pd
client = MongoClient() # Remember your uri string
col = client['test']['test']
df = pd.DataFrame({'name': ['Braund','Cummings','Heikkinen','Allen'],
'age': [22,38,26,35],
'fare': [7.25, 71.83, 0 , 8.05],
'survived?': [False, True, True, False]})
data = df.to_dict(orient='records') # Here's our added param..
col.insert_many(data)
简而言之,通过指定orient='records'
输出为dict列表,即接受的相同格式insert_many
。另外,作为奖励- to_dict
与to_json
!相比,使用datetime类型的列效果更好!