I have been attempting to insert the rows of a pandas dataframe into a mongodb collection as individual documents. I am pulling the data from MongoDB using pymongo, performing some transformations, running a scoring algorithm, and adding the score as an additional column to the dataframe. The last step will be to insert the rows into a special collection on the mongoDB database as individual documents but I am completely stuck. My example dataframe df looks like this.
memberID dxCodes dxCount score
0 856589080 [4280, 4293, 4241, 4240, 4242, 4243] 6 1.8
1 906903383 [V7612] 1 2.6
2 837210554 [4550, 4553, V1582] 3 3.1
3 935634391 [78791, 28860, V1582, 496, 25000, 4019] 6 1.1
4 929185103 [30500, 42731, 4280, 496, 59972, 4019, 3051] 7 2.8
MemberID is a string, dx codes would be an array (in MongoDB terminology), dxCount is an int, and score is a float. I have been toying around with a piece of code I found posted in response to a vaguely similar question.
import json
import datetime
df = pandas.DataFrame.from_dict({'A': {1: datetime.datetime.now()}})
records = json.loads(df.T.to_json()).values()
db.temp.insert_many(records)
This is what I was able to get in my collection:
{
"_id" : ObjectId("565a8f206d8bc51a08745de0"),
"A" : NumberLong(1448753856695)
}
It's not much, but its as close as I have gotten. I have spent a lot of time googling and taking shots in the dark but haven't cracked it yet. Any guidance is greatly appreciated, thanks in advance for your assistance!