使用PyMongo将Pandas Dataframe插入mongodb [英] Insert a Pandas Dataframe into mongodb using PyMongo

查看:1326
本文介绍了使用PyMongo将Pandas Dataframe插入mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PyMongo将pandas DataFrame插入mongodb的最快方法是什么?

What is the quickest way to insert a pandas DataFrame into mongodb using PyMongo?

尝试

db.myCollection.insert(df.to_dict())

出现错误

InvalidDocument: documents must have only string keys, the key was Timestamp('2013-11-23 13:31:00', tz=None)

InvalidDocument: documents must have only string keys, the key was Timestamp('2013-11-23 13:31:00', tz=None)


 db.myCollection.insert(df.to_json())

出现错误

TypeError: 'str' object does not support item assignment


 db.myCollection.insert({id: df.to_json()})

出现错误

InvalidDocument: documents must have only string a keys, key was <built-in function id>

df

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 150 entries, 2013-11-23 13:31:26 to 2013-11-23 13:24:07
Data columns (total 3 columns):
amount    150  non-null values
price     150  non-null values
tid       150  non-null values
dtypes: float64(2), int64(1)

推荐答案

我怀疑是否同时有 quickest simple 方法.如果您不担心数据转换,可以这样做

I doubt there is a both quickest and simple method. If you don't worry about data conversion, you can do

>>> import json
>>> df = pd.DataFrame.from_dict({'A': {1: datetime.datetime.now()}})
>>> df
                           A
1 2013-11-23 21:14:34.118531

>>> records = json.loads(df.T.to_json()).values()
>>> db.myCollection.insert(records)

但是如果您尝试将数据加载回 ,您将获得:

But in case you try to load data back, you'll get:

>>> df = read_mongo(db, 'myCollection')
>>> df
                     A
0  1385241274118531000
>>> df.dtypes
A    int64
dtype: object

,因此您必须将'A'列转换为datetime,以及DataFrame中所有非intfloatstr字段.对于此示例:

so you'll have to convert 'A' columnt back to datetimes, as well as all not int, float or str fields in your DataFrame. For this example:

>>> df['A'] = pd.to_datetime(df['A'])
>>> df
                           A
0 2013-11-23 21:14:34.118531

这篇关于使用PyMongo将Pandas Dataframe插入mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆