使用PyMongo
將最簡單的方法將pandas DataFrame插入到mongodb中是什麼?使用PyMongo將熊貓數據框插入到mongodb
嘗試
db.myCollection.insert(df.to_dict())
給了一個錯誤InvalidDocument: documents must have only string keys, 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 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)
之後你想做什麼?你需要每個記錄一個文檔還是每個數據框一個文檔? – alko
每個mongo記錄都有字段'date','amount','price'和tid。 'tid'應該是一個唯一的字段 – Nyxynyx