从MongoDB字典到数据框映射

时间:2019-12-23 17:02:32

标签: pandas mongodb dataframe dictionary

在我的previous question中,我将DF转换为字典列表,以上传到MongoDB。

现在我正在做相反的工作。从MongoDB查询中,我可以下载包含以下信息的字典列表:

[
{Info1: 3,
 City: BCN,
 Country: Spain},

{Info2: 5.6,
 City: BCN,
 Country: Spain},

{Info1: 4,
 City: Moscow,
 Country: Russia},

{Info2: 7,
 City: Moscow,
 Country: Russia}
]

现在我要创建一个表,如下所示:

City    Country   Info1  Info2
BCN      Spain    3      5.6   
Moscow   Russia   4      7   

我现在的操作方式如下:

  def generate_excel(ind_type):
     # first add columns
     columns = ["City", "Country"]

     # then  find all indictors filtered
     indicators = []
     for indicator in CUSTOMERS_COLLECTION.find().distinct("ID"):
         indicators.append(indicator)

     # then add the indicators in column
     columns = columns + indicators

     # First find all Ciudades
     cities = CUSTOMERS_COLLECTION.find()

      rows_list = []
     for ciudad in cities.distinct("City"):
        indicators = CUSTOMERS_COLLECTION.find({"City": ciudad})
        dict_ind = {}
        # then we create a dict of the indicators. It will be the row
        for indicator in indicators:
            dict_ind[indicator["ID"]] = indicator["Valor"]
            dict_ind["Country"] = indicator["Country"]
            dict_ind["City"] = indicator["City"]

     df_ = pd.DataFrame(rows_list, columns=columns)
     return df_

正如我之前的问题一样,此方法有效,但似乎根本没有优化。 MongoDB或DF是否有任何功能可以正确映射字典?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不是100%会为您工作,但是过去我已经能够将mongo查询简单地转换为数据帧。例如:

q1=db.collection.find(#add whatever filters you need)
df= pd.DataFrame(q1)

请告诉我这是否有效。