'Pandas dataframe to mongoDB document

I have a pandas dataframe that I import to a MongoDB database.

Each row of the dataframe is transformed into a document like bellow.

    {
        title: 'Spacious bachelor apartment',
        status: 'rented',
        modified_at: ISODate("2013-04-01T10:37:38.000Z"),
        property_object_id: '12345',
        address: 'street 1, Place, Country',
        bathrooms: '2',
        bedrooms: '3',
        construction_year: '2001',
        aircondition: 'yes',
        aircondition_number: '4',
        airy: 'no',
        alarm: 'no',
        balcony: '',
        balcony_size: null,
}

This is done by transforming the dataframe with

df.to_dict("records")

And then inserting_many to the collection in MongoDB database.

My question is if I need to transform the data to have a final schema as bellow:

{
    general:{
        title: 'Spacious bachelor apartment',
        status: 'rented',
        modified_at: ISODate("2013-04-01T10:37:38.000Z"),
        property_object_id: '12345',
        address: 'street 1, Place, Country',
        },
    basic: {
        bathrooms: '2',
        bedrooms: '3',
        construction_year: '2001',
        },
    amenities:{
        aircondition: 'yes',
        aircondition_number: '4',
        airy: 'no',
        alarm: 'no',
        balcony: '',
        balcony_size: null,
        },
}

Where and how do I create this schema?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source