pandas df to mongodb 1

pandas df to mongodb

db.myCollection.insert_many(df.apply(lambda x: x.to_dict(), axis=1).to_list())

Here is what the above code is Doing:
1. We’re using the apply method to apply a function to each row of the dataframe.
2. The function we’re applying is a lambda function that converts each row to a dictionary.
3. We’re using the to_list method to convert the result to a list.
4. We’re using the insert_many method to insert the list of dictionaries into the collection.

Similar Posts