[Python] Pandas dataframe 資料儲存至 MongoDB

本篇文章主要為將資料讀取至 DataFrame 後,做完前處理將 DataFrame 中的資料儲存至 MongoDB。


前置步驟

  1. 需要先安裝 MongoDB
  2. 透過 pip install 安裝 pandas 及 pymongo lib
pip install pandas  
pip install pymongo  

執行程式碼

import pandas as pd # 引用套件並縮寫為 pd  
from pymongo import MongoClient


def data_to_dataframe():

    groups = ["Movies", "Sports", "Coding", "Fishing", "Dancing", "cooking"]
    num = [46, 8, 12, 12, 6, 58]

    dict = {"groups": groups,
            "num": num
           }

    select_df = pd.DataFrame(dict)
    dataframe_to_mongo(select_df)


def dataframe_to_mongo(select_df):

    client = MongoClient()
    database = client["db_name"]  # SQL: Database Name
    collection = database["collection"]   # SQL: Table Name


    records = select_df.to_dict('records') # 參數 record 代表把列轉成個別物件
    collection.insert_many(records)


def main():  
    data_to_dataframe()


if __name__ == "__main__":  
    main()
系統環境
  • Ubuntu 16.04
  • Python 2.7.8
ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io