출처: https://towardsdatascience.com/pandas-vs-polars-a-syntax-and-speed-comparison-5aa54e27497e
Pandas vs. Polars: A Syntax and Speed Comparison
Understanding the major differences between the Python libraries Pandas and Polars for Data Science
towardsdatascience.com
170만 행을 가진 텍스트 데이터를 읽어올 일이 생겼는데 평소처럼 pd.read_XXX 를 진행했더니 하나 읽는 데만 한 세월.
옆자리 대리님께서(그저 빛) polars 라는 라이브러리가 있는데, 데이터 읽어오는 속도가 pandas의 약 8배 빠르다고.
이런건 못참지 싶어 얼른 설치후 사용!!
pip 만 사용해서 바로 설치할 수 있어 간편했다.
!pip install polars
라이브러리를 찾아보니 엑셀, csv, json 등 판다스에서 지원하는 건 다 되는듯.
https://pola-rs.github.io/polars-book/user-guide/dsl/expressions.html
근데 컬럼 조회나 인덱싱 같은게 pandas보다 직관적이진 못함. 위에 사이트에 따르면 pandas와 polars에서 같은 기능을 실행하기위한 코드는 아래와 같이 차이난다.
# Pandas
df[['col1', 'col2']]
# The above code will run with Polars as well,
# but the correct way in Polars is:
df.select(pl.col(['col1', 'col2']))
그리고 실제로 필터링 기능/ 새 컬럼 생성 시 걸리는 시간 효율은 polars 보단 pandas 가 더 낫다고 함.
그래서 파일 읽어오는 것만 polars로 하고, pandas DataFrame 형식으로 변환해서 사용중. 덕분에 데이터 처리속도가 상당히 빨라졌다.
df = pl.read_csv('file.csv')
df = df.to_pandas()
결론: 읽어오는건 polars로, 필터링, 셀렉팅, 새 컬럼 생성 등은 pandas로.
(사실 셀렉팅도 polars가 빠르긴 한데 문법이 어렵기도 하고 다른 기능에 비해 큰 시간 효율 차이가 안남
'공부 > Python' 카테고리의 다른 글
파케이, 컬럼 기반/ 행 기반 데이터 포맷 (0) | 2023.08.09 |
---|---|
주피터 노트북 메모리 표시 모듈 추가 (0) | 2023.07.31 |
Iterable / Iterator (0) | 2021.11.18 |