Python Forum
Pandas and MongoDB question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas and MongoDB question
#1
Hi, I am new to pandas and MongoDB. This is the problem I have:
I have some data stored in MongoDB. My database is simply called 'mydata'. I have two files in the collection: file1 and file2.
So I my program only needs to connect to MongoDB and load file1 which is in bson format. I want to read file1 into a dataframe- df and carry out a certain operation which involves the removal of certain data from one of the columns. I have worked out how to do that. I now need to save the modified dataframe to mongoDB in such a way that it overwrites / replaces the original file1.
I can't work out how to do this. Please can anyone help me? Thanks
Reply
#2
To handle your problem, you can use the pymongo library to interact with MongoDB and pandas to process the data as a DataFrame. First, establish a connection to MongoDB using pymongo and locate the file1 collection within your mydata database. Retrieve the data stored in BSON format and convert it into a pandas DataFrame for easier manipulation. Once the data is loaded, perform your desired operation on the DataFrame, such as filtering out specific values from a column or modifying the data as needed. After completing the modification, you need to overwrite the existing content in file1 with the updated data. To do this, first convert the modified DataFrame back into a dictionary using the .to_dict(orient='records') method, which prepares the data for MongoDB insertion. Then, use the delete_many({}) method from pymongo to clear the existing data in the file1 collection and follow up with insert_many() to insert the updated records. This approach ensures that the original data is completely replaced with the modified content in MongoDB, effectively overwriting it with the new data derived from your DataFrame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas.json_normalize question elsvieta 6 430 Apr-04-2025, 03:47 PM
Last Post: Pedroski55
  building a chatbot that fetches data from mongodb using RAG saisankalpj 0 938 Jul-10-2024, 04:49 PM
Last Post: saisankalpj
  pandas df inside a df question mbaker_wv 4 2,122 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Pandas usecols question rsearing 1 1,899 Aug-20-2022, 10:10 PM
Last Post: jefsummers
  Simple pandas question mcva 4 3,647 Dec-17-2021, 04:47 PM
Last Post: mcva
  mongodb beatles data scraping question darter 0 1,580 Oct-30-2021, 12:02 AM
Last Post: darter
  Pandas question new2datasci 0 2,444 Jan-10-2021, 01:29 AM
Last Post: new2datasci
  Pandas merge question smw10c 2 6,475 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  Counting Criteria in Pandas Question Koenig 1 2,714 Sep-30-2019, 05:16 AM
Last Post: perfringo
  Function question using Pandas smw10c 7 8,600 Feb-12-2019, 06:52 PM
Last Post: Nathandsn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020