티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to reversibly store and load a Pandas dataframe to/from disk

Pandas 데이터프레임을 디스크에 저장하고 로드하는 방법

 문제 내용 

Right now I'm importing a fairly large CSV as a dataframe every time I run the script. Is there a good solution for keeping that dataframe constantly available in between runs so I don't have to spend all that time waiting for the script to run?

현재 스크립트를 실행할 때마다 상당히 큰 CSV 파일을 데이터프레임으로 가져오고 있습니다. 실행 시간을 기다리는 시간이 많이 소요되지 않도록 데이터프레임을 계속 사용 가능한 상태로 유지하는 좋은 해결책이 있을까요?

 

 

 

 높은 점수를 받은 Solution 

The easiest way is to pickle it using to_pickle:

가장 쉬운 방법은 to_pickle을 사용하여 피클 형식으로 저장하는 것입니다.
df.to_pickle(file_name)  # where to save it, usually as a .pkl

 

Then you can load it back using:

그런 다음, 다음과 같이 로드할 수 있습니다.
df = pd.read_pickle(file_name)

 

Note: before 0.11.1 save and load were the only way to do this (they are now deprecated in favor of to_pickle and read_pickle respectively).

참고: 0.11.1 이전에는 저장하고 로드하는 것이 유일한 방법이었지만 (지금은 to_pickle과 read_pickle을 사용하는 것이 권장됩니다).

 


 

Another popular choice is to use HDF5 (pytables) which offers very fast access times for large datasets:

또 다른 인기 있는 선택은 대용량 데이터셋에 대한 매우 빠른 액세스 시간을 제공하는 HDF5(pytables)를 사용하는 것입니다.
import pandas as pd
store = pd.HDFStore('store.h5')

store['df'] = df  # save it
store['df']  # load it

 

More advanced strategies are discussed in the cookbook.

고급 전략은 쿡북에서 논의됩니다.

 


 

Since 0.13 there's also msgpack which may be be better for interoperability, as a faster alternative to JSON, or if you have python object/text-heavy data (see this question).

0.13 버전 이후로는 msgpack도 있으며 JSON의 빠른 대안 또는 Python 객체/텍스트 중심 데이터가 있는 경우 상호 운용성이 더 좋을 수 있습니다(이 질문 참조).

 

 

 

 가장 최근 달린 Solution 

As already mentioned there are different options and file formats (HDF5, JSON, CSV, parquet, SQL) to store a data frame. However, pickle is not a first-class citizen (depending on your setup), because:

이미 언급된 대로 데이터프레임을 저장하는 데에는 다양한 옵션과 파일 형식(HDF5, JSON, CSV, parquet, SQL)이 있습니다. 그러나 피클은 (설정에 따라) 일등 시민이 아닐 수 있습니다. 이는 다음과 같은 이유 때문입니다:

 

  1. pickle is a potential security risk. Form the Python documentation for pickle:
1. pickle은 잠재적인 보안 위협이 될 수 있습니다. Python의 pickle 문서에서는 다음과 같이 설명합니다:

 

Warning The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.

경고: pickle 모듈은 잘못 구성된 또는 악의적으로 구성된 데이터에 대해 안전하지 않습니다. 신뢰할 수 없거나 인증되지 않은 출처에서 수신한 데이터를 unpickle하면 안됩니다.

 

  1. pickle is slow. Find here and here benchmarks.
2. pickle은 느립니다. 여기와 여기에서 벤치마크를 찾을 수 있습니다.

 

Depending on your setup/usage both limitations do not apply, but I would not recommend pickle as the default persistence for pandas data frames.

설정이나 사용 방법에 따라서는 이러한 제한이 적용되지 않을 수 있지만, pandas 데이터 프레임의 기본 지속성으로 pickle을 권장하지는 않습니다.

 

 

 

출처 : https://stackoverflow.com/questions/17098654/how-to-reversibly-store-and-load-a-pandas-dataframe-to-from-disk

반응형
댓글
공지사항
최근에 올라온 글