简体   繁体   中英

Dictionary with dataframes as values to Mongo; .to_dict not an option, any way to convert?

I have a dictionary containing 10 keys where the values are pandas dataframes; I need to upload the data to a Mongo database but I cannot convert the dataframes using .to_dict(orient='records'). Can anyone please enlighten me on how I could convert and use my data with Mongo?

I created a sample code, I need to convert the dictionary mydict below to a usable format:

df1=pd.DataFrame({'j':[34,54,55],'k'[4,5,6]})
df2=pd.DataFrame({'m':[3,8,5],'n':[2,3,4]})

mydict={'key1':df1,'key2':df2}

since df1 and df2 are dataframes and are within a dictionary, .to_dict is not an option, I also tried looping and converting but cannot think of any other ways.

Is it this what you are looking for:

targetdict = {}
for k in mydict:
    targetdict[k] = mydict[k].to_dict()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM