0

The default Dataframe.to_json() exports like this:

{
    "date": {
        "0": Example_date1,
        "1": Example_date2
    }
    "name": {
        "0": Example_name1,
        "1": Example_name2,
    }
}

I want the json to be document oriented for MongoDB, like this:

{
    {
        "date": Example_date1,
        "name": Example_name1
    }
    {
        "date": Example_date2,
        "name": Example_name2,
    }
}

I got this problem, because I want to read data from MongoDB database and when I load into a Dataframe, it gets only one line. I think it's better to send a json document oriented. I am new to working with MongoDB and NoSQL structures, so if you have a suggestion I'd be happy to read it.

1

1 Answer 1

0

If you use .to_dict(orient="records") you get this output:

[
    {'date': 'Example_date1', 'name': 'Example_name1'}, 
    {'date': 'Example_date2', 'name': 'Example_name2'}
]

If you really needed the mentioned format then I suggest this:

dict_df = df.to_dict(orient="records")
formatted_dict = str(dict_df).replace("[", "{").replace("]", "}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.