본문 바로가기
반응형

Python185

Pandas의 read_csv 함수 느린 속도 개선하기 Pandas의 read_csv 함수의 느린 속도를 개선하는 방법을 다룹니다. 2022. 03. 11 최초작성 csv 파일을 하나 읽어서 작업할 때에는 Pandas의 read_csv 함수가 느리다는 것을 알지 못했는데 대량의 csv 파일을(정확히는 196,032개) 로드해보니 느리다는 것을 알 수 있었습니다. 개선할 방법을 찾아보니 read_csv의 engine 아규먼트에 pyarrow를 지정하는 방법이 있었습니다. 앞에서 했던 196,032개의 csv 파일을 로드하는 시간이 3분에 1로 감소했습니다. df = pd.read_csv("large.csv", engine="pyarrow") 속도는 빨라지지만 단점이 있다면 기존 read_csv와 완벽히 호환이 안되서 nrows 같은 아규먼트를 사용할 수 없습니.. Python/Pandas 2023. 10. 7.
Matplotlib 예제 – 하나의 figure에 여러 개의 이미지 출력하기 하나의 figure안에 여러 개의 이미지를 출력하는 예제코드입니다. figure 안에 여러 개의 subplot이 존재할 수 있고 subplot마다 하나의 그래프나 이미지가 그려지게 됩니다. 2022. 10. 19 최초작성 import cv2 import numpy as np import matplotlib.pyplot as plt # 가로 길이, 세로 길이. 인치 단위 plt.figure(figsize=(7, 10)) for i in range(20): # 열개수 4개, 행 개수 5개 짜리 그리드를 생성한 후, 인덱스 i+1 번째를 사용 plt.subplot(5, 4, i + 1) # 빈 이미지에 인덱스 숫자를 출력 img_empty = np.zeros((150,150,1), dtype=np.uint8).. Python/Matplotlib 2023. 10. 7.
Pandas drop 예제 Pandas에서 drop을 사용하여 DataFrame의 열 또는 행을 삭제하는 예제입니다. 2022. 10. 31 최초작성 import pandas as pd df = pd.DataFrame({ 'alphabet': ['a', 'b', 'c', 'd'], 'integer' : [1, 2, 3, 4], 'blood type': ['A', 'B', 'AB', 'O'], }) print(df) ''' alphabet integer blood type 0 a 1 A 1 b 2 B 2 c 3 AB 3 d 4 O ''' # 행을 지우려면 행 인덱스 번호와 axis=0을 사용해야 합니다. # 두번째, 세번째 행이 삭제됩니다. df = df.drop([1,2], axis=0) print(df) ''' alphabet .. Python/Pandas 2023. 10. 7.
해결방법 ValueError: No template sub-directory with name ‘script’ found in the following paths MacBook에서 ipynb를 py로 변환시 발생한 다음 에러를 해결한 방법입니다. ValueError: No template sub-directory with name ‘script’ found in the following paths 2023. 9. 24 최초작성 MacBook에서 주피터 노트북 파일 ipynb을 파이썬 코드 파일 py로 바꾸려고 했는데 에러가 발생했습니다. % jupyter nbconvert --to script faiss.ipynb .... ValueError: No template sub-directory with name 'script' found in the following paths: /Users/webnautes/Library/Jupyter /Users/webnautes.. Python/Python 개발환경 2023. 10. 7.
csv 파일을 여러 개 파일로 분리하는 python 예제 csv 파일을 일정 크기로 나누어 여러 파일로 분리하는 파이썬 예제 코드입니다. 최초작성 2023. 9. 28 다음 링크에 있는 iris 데이터셋을 가지고 진행했습니다. https://www.kaggle.com/datasets/arshid/iris-flower-dataset/ 전체 소스코드입니다. import pandas as pd import numpy as np file = 'IRIS.csv' filename = file.split('.')[0] df = pd.read_csv(file) print(df.shape) print() print(df.head()) print() n = 50 for idx,(_, sub_df) in enumerate(df.groupby(np.arange(len(df)) //.. Python/Python 예제 코드 2023. 10. 7.
반응형