将pandas数据帧插入mongodb

时间:2018-01-05 05:11:55

标签: python pandas dataframe pymongo

将数据帧重新索引到dict,我想将数据插入mongodb新的Collection? 数据帧:

df = mydf.groupby(pd.TimeGrouper(key='timestamp', freq='5Min'))['value'].apply(lambda x: x[x!='-1'].count())
k = df.reset_index().T.to_dict().values()
print(k)

p={}
p[name] = k #adding new key into json
print(p)

db.col8.insert_many(k)  #inserting data into mongodb

错误:

TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

预期结果:

{
        "_id" : ObjectId("5a4f07559799980e94717c69"),
        "name": "abc"
        "timestamp" : ISODate("2018-01-03T12:00:00Z"),
        "value" : 0
}

插入不应该重复它应该upsert.So如何使用pandas dataframe框架mongodb查询插入/更新?

1 个答案:

答案 0 :(得分:0)

为了将来,您始终需要将变量类型从熊猫转换为简单的Python类型:dictliststrintfloat,等等。所有numpy / pandas类型都会引发异常。

固定代码,我想您想向数据库中插入p而不是k变量:

df = mydf.groupby(pd.TimeGrouper(key='timestamp', freq='5Min'))['value'].apply(lambda x: x[x!='-1'].count())
k = df.reset_index().T.to_dict().values()
print(k)

p={}
p[name] = k #adding new key into json
print(p)

db.col8.insert_many(p)  #inserting data into mongodb