ProgramingTip

mongodb에서 pandas로 데이터를 가져 오는 방법은 무엇입니까?

bestdevel 2020. 10. 4. 12:05
반응형

mongodb에서 pandas로 데이터를 가져 오는 방법은 무엇입니까?


mongodb의 컬렉션에 분석해야 할 많은 양의 데이터가 있습니다. 해당 데이터를 Pandas로 어떻게 가져 옵니까?

나는 pandas와 numpy를 처음 사용합니다.

편집 : mongodb 컬렉션에는 날짜 및 시간 태그가 지정된 센서 값이 포함되어 있습니다. 센서 값은 float 데이터 유형입니다.

샘플 데이터 :

{
"_cls" : "SensorReport",
"_id" : ObjectId("515a963b78f6a035d9fa531b"),
"_types" : [
    "SensorReport"
],
"Readings" : [
    {
        "a" : 0.958069536790466,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:26:35.297Z"),
        "b" : 6.296118156595,
        "_cls" : "Reading"
    },
    {
        "a" : 0.95574014778624,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:27:09.963Z"),
        "b" : 6.29651468650064,
        "_cls" : "Reading"
    },
    {
        "a" : 0.953648289182713,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:27:37.545Z"),
        "b" : 7.29679823731148,
        "_cls" : "Reading"
    },
    {
        "a" : 0.955931884300997,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:28:21.369Z"),
        "b" : 6.29642922525632,
        "_cls" : "Reading"
    },
    {
        "a" : 0.95821381,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:41:20.801Z"),
        "b" : 7.28956613,
        "_cls" : "Reading"
    },
    {
        "a" : 4.95821335,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:41:36.931Z"),
        "b" : 6.28956574,
        "_cls" : "Reading"
    },
    {
        "a" : 9.95821341,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:42:09.971Z"),
        "b" : 0.28956488,
        "_cls" : "Reading"
    },
    {
        "a" : 1.95667927,
        "_types" : [
            "Reading"
        ],
        "ReadingUpdatedDate" : ISODate("2013-04-02T08:43:55.463Z"),
        "b" : 0.29115237,
        "_cls" : "Reading"
    }
],
"latestReportTime" : ISODate("2013-04-02T08:43:55.463Z"),
"sensorName" : "56847890-0",
"reportCount" : 8
}

pymongo 다음은 내가 사용하는 코드입니다.

import pandas as pd
from pymongo import MongoClient


def _connect_mongo(host, port, username, password, db):
    """ A util for making a connection to mongo """

    if username and password:
        mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db)
        conn = MongoClient(mongo_uri)
    else:
        conn = MongoClient(host, port)


    return conn[db]


def read_mongo(db, collection, query={}, host='localhost', port=27017, username=None, password=None, no_id=True):
    """ Read from Mongo and Store into DataFrame """

    # Connect to MongoDB
    db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)

    # Make a query to the specific DB and Collection
    cursor = db[collection].find(query)

    # Expand the cursor and construct the DataFrame
    df =  pd.DataFrame(list(cursor))

    # Delete the _id
    if no_id:
        del df['_id']

    return df

이 코드를 사용하여 mongodb 데이터를 pandas DataFrame에로드 할 수 있습니다. 그것은 나를 위해 작동합니다. 당신도 희망합니다.

import pymongo
import pandas as pd
from pymongo import MongoClient
client = MongoClient()
db = client.database_name
collection = db.collection_name
data = pd.DataFrame(list(collection.find()))

Monary정확하게 수행하고 매우 빠 사용 . ( 다른 링크 )

포함 된 안내와 몇 가지 타이밍이 포함 된 게시물참조하십시오 .


PEP에 따르면 단순한 복잡한 것보다 낫습니다.

import pandas as pd
df = pd.DataFrame.from_records(db.<database_name>.<collection_name>.find())

일반 mongoDB 데이터베이스로 작업하는 것처럼 조건을 포함하거나 find_one ()을 사용하여 데이터베이스에서 하나의 요소 만 가져 오는 등의 작업을 수행 할 수 있습니다.

그리고 짜잔!


import pandas as pd
from odo import odo

data = odo('mongodb://localhost/db::collection', pd.DataFrame)


Out-of-core (RAM에 맞지 발생) 데이터를 처음으로 (즉, 송신 실행으로) 처리하기 위해 Python Blaze 생태계 ( Blaze / Dask / Odo)를 볼 수 있습니다 .

Blaze (및 Odo )에는 MongoDB를 처리하는 기본 기능이 있습니다.

시작하기위한 몇 가지 유용한 가이드 :

Blaze 스택으로 가능한 놀라운 일을 보여주는 기사 : Blaze 및 Impala를 사용하여 17 억 개의 Reddit 댓글 분석 (본질적으로 975Gb의 Reddit 댓글을 몇 초 만에 쿼리).

PS 저는 이러한 기술과 관련이 없습니다.


매우 유용한 또 다른 옵션은 다음과 같습니다.

from pandas.io.json import json_normalize

cursor = my_collection.find()
df = json_normalize(cursor)

이렇게하면 중첩 된 mongodb 문서를 무료로 펼칠 수 있습니다.


사용

pandas.DataFrame(list(...))

반복기 / 생성기 결과가 크면 많은 메모리를 소비합니다.

작은 청크를 생성하고 끝에 연결하는 것이 좋습니다.

def iterator2dataframes(iterator, chunk_size: int):
  """Turn an iterator into multiple small pandas.DataFrame

  This is a balance between memory and efficiency
  """
  records = []
  frames = []
  for i, record in enumerate(iterator):
    records.append(record)
    if i % chunk_size == chunk_size - 1:
      frames.append(pd.DataFrame(records))
      records = []
  if records:
    frames.append(pd.DataFrame(records))
  return pd.concat(frames)

http://docs.mongodb.org/manual/reference/mongoexport

csv로 내보내기 및 사용 read_csv또는 JSON 및 사용DataFrame.from_records


waitkuo 의이 훌륭한 답변에 따라 .read_sql ().read_csv ()함께 chunksize를 사용하여 수행 할 가능성을 추가하고 싶습니다 . 나는 '반복자'/ '커서'의 '레코드'를 하나씩 이동하는 것을 피함으로써 Deu Leung 의 대답을 확대합니다 . 이전 read_mongo 함수 를 빌릴 것 입니다.

def read_mongo(db, 
           collection, query={}, 
           host='localhost', port=27017, 
           username=None, password=None,
           chunksize = 100, no_id=True):
""" Read from Mongo and Store into DataFrame """


# Connect to MongoDB
#db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)
client = MongoClient(host=host, port=port)
# Make a query to the specific DB and Collection
db_aux = client[db]


# Some variables to create the chunks
skips_variable = range(0, db_aux[collection].find(query).count(), int(chunksize))
if len(skips_variable)<=1:
    skips_variable = [0,len(skips_variable)]

# Iteration to create the dataframe in chunks.
for i in range(1,len(skips_variable)):

    # Expand the cursor and construct the DataFrame
    #df_aux =pd.DataFrame(list(cursor_aux[skips_variable[i-1]:skips_variable[i]]))
    df_aux =pd.DataFrame(list(db_aux[collection].find(query)[skips_variable[i-1]:skips_variable[i]]))

    if no_id:
        del df_aux['_id']

    # Concatenate the chunks into a unique df
    if 'df' not in locals():
        df =  df_aux
    else:
        df = pd.concat([df, df_aux], ignore_index=True)

return df

Rafael Valero, waitingkuo 및 Deu Leung과 같은 유사한 접근 방식은 페이지 매김을 사용합니다 .

def read_mongo(
       # db, 
       collection, query=None, 
       # host='localhost', port=27017, username=None, password=None,
       chunksize = 100, page_num=1, no_id=True):

    # Connect to MongoDB
    db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)

    # Calculate number of documents to skip
    skips = chunksize * (page_num - 1)

    # Sorry, this is in spanish
    # https://www.toptal.com/python/c%C3%B3digo-buggy-python-los-10-errores-m%C3%A1s-comunes-que-cometen-los-desarrolladores-python/es
    if not query:
        query = {}

    # Make a query to the specific DB and Collection
    cursor = db[collection].find(query).skip(skips).limit(chunksize)

    # Expand the cursor and construct the DataFrame
    df =  pd.DataFrame(list(cursor))

    # Delete the _id
    if no_id:
        del df['_id']

    return df

참고 URL : https://stackoverflow.com/questions/16249736/how-to-import-data-from-mongodb-to-pandas

반응형