Cách đọc tệp csv lớn bằng Python

Chọn một trang web để nhận nội dung đã dịch nếu có và xem các sự kiện và ưu đãi tại địa phương. Dựa trên vị trí của bạn, chúng tôi khuyên bạn nên chọn.

Bạn cũng có thể chọn một trang web từ danh sách sau

Làm thế nào để có được hiệu suất trang web tốt nhất

Chọn trang Trung Quốc [bằng tiếng Trung hoặc tiếng Anh] để có hiệu suất trang tốt nhất. Các trang web quốc gia khác của MathWorks không được tối ưu hóa cho các lượt truy cập từ vị trí của bạn

Nếu bạn có tệp CSV rất lớn khiến bạn gặp sự cố khi cập nhật. Bạn có thể chia Dữ liệu bằng Python thành nhiều Sổ làm việc hơn. Nó nhanh hơn Macro VBA và thực hiện quy trình với ít dòng mã hơn. Sử dụng lệnh Chunk từ Thư viện Pandas

Thật dễ dàng để tải xuống Python nếu bạn chưa cài đặt nó, hãy truy cập https. //www. con trăn. org/ và sau đó cài đặt Thư viện Pandas bằng Cửa sổ Dấu nhắc Lệnh, Nhập. pip cài đặt gấu trúc. Hay nhất là sử dụng mã Python miễn phí

Sao chép Mã bên dưới và đổi tên Sổ làm việc CSV thành tên sổ làm việc của bạn, sau đó chạy mã từ IDLE. Chỉ cần nhập IDLE từ trường tìm kiếm bên dưới ở góc dưới cùng bên trái của Windows. Thay đổi kích thước Chunk thành số hàng bạn muốn có trên mỗi tệp

TLDR. So sánh hiệu suất của 4 cách khác nhau để đọc tệp CSV lớn bằng Python. Tìm phương pháp phù hợp nhất với trường hợp sử dụng của bạn. Dấu. tất cả các phương pháp đều sử dụng trình tạo của Python bằng cách nào đó

Tại sao lại so sánh các cách đọc tệp CSV khác nhau?

Đọc CSV là trường hợp sử dụng rất phổ biến khi Python tiếp tục phát triển trong cộng đồng phân tích dữ liệu. Dữ liệu cũng đang phát triển và thường xảy ra trường hợp tất cả dữ liệu mà mọi người đang cố gắng xử lý sẽ không vừa với bộ nhớ

Không phải lúc nào cũng cần tải tất cả dữ liệu vào bộ nhớ. Chúng ta có thể sử dụng các trình tạo trong Python để lặp qua các tệp lớn theo khối hoặc theo từng hàng

Cuộc thí nghiệm

Chúng tôi sẽ tạo tệp CSV có 10 triệu hàng, rộng 15 cột, chứa các số nguyên lớn ngẫu nhiên. Tệp này đối với tôi là khoảng 1. 3GB, không quá lớn, nhưng đủ lớn cho các thử nghiệm của chúng tôi

Mỗi cách tiếp cận sẽ đọc toàn bộ CSV và tính tổng của cột ở chỉ mục 2

Máy của tôi là Máy tính để bàn Windows 10 với RAM 16 GB, sử dụng AMD Ryzen 5 với 6 lõi [12 logic]

Đây là tập lệnh tôi đã sử dụng để tạo

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
0

import pandas as pd 
import numpy as np

df = pd.DataFrame[data=np.random.randint[99999, 99999999, size=[10000000,14]]]

df.to_csv["/mnt/c/data/huge_data.csv"]

Sau đó, tôi đã sử dụng mô-đun

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
1 để tính thời gian thực thi toàn bộ tập lệnh cho từng phương pháp đọc tệp CSV lớn

Bốn cách để đọc tệp CSV lớn bằng Python

Trăn tinh

Cách tiếp cận này không sử dụng thư viện bổ sung. Dưới mui xe,

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
2 đang sử dụng trình tạo để đọc từng dòng một

Thời gian. 12. 13 giây

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]

Trình đọc CSV

Ở đây, chúng tôi sử dụng mô-đun

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
3 phổ biến để phân tích cú pháp tệp đang mở và sử dụng trình tạo của nó để lặp lại. Tôi không hoàn toàn chắc chắn tại sao hiệu suất lại bị ảnh hưởng nhưng đáng để tìm hiểu xem mô-đun
import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
3 có thể bổ sung chi phí nào nếu bạn yêu cầu hiệu suất cao

Thời gian. 26. 32 giây

import csv
import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        reader = csv.reader[csv_file]
        if skip_header:
            next[reader, None]
        for row in reader:
            try:
                total += int[row[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
    return total
            

data_sum = read_data[FILE_PATH, 2]
print[data_sum]
print[f"Done in {time.time[]-start} seconds"]

Pandas với chunksize

Ở đây chúng tôi sử dụng

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
0 để tạo nên một tập lệnh rất ngắn. Nếu bạn đã có
import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
0 trong dự án của mình, bạn có thể sử dụng phương pháp này để đơn giản hóa

Chúng tôi chỉ định một

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
2 để
import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
3 không đọc toàn bộ CSV vào bộ nhớ. Mỗi đoạn là một khung dữ liệu

Thời gian. số 8. 81 giây

import pandas as pd
import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"


def sum_column[chunk, column_idx]:
    return chunk.iloc[:,column_idx].sum[]

chunksize = 10 ** 6
total_sum = 0
column_index = 2
with pd.read_csv[FILE_PATH, chunksize=chunksize] as reader:
    for chunk in reader:
        total_sum += sum_column[chunk, column_index]

print[f"Total: {total_sum}"]
print[f"Done in {time.time[]-start} seconds"

Đa xử lý sau khi chia nhỏ tệp

Đây là phương pháp phức tạp nhất nhưng có hiệu suất tốt nhất, giả sử trường hợp sử dụng xử lý CSV của bạn có thể cho phép chia nhỏ tệp

Bạn có thể chia tệp trực tiếp trước khi sử dụng tập lệnh Python của mình nhưng tôi bao gồm lệnh gọi

import time
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        total = 0
        if skip_header:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                total += int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]
        return total
            

data_total = read_data[FILE_PATH, 2]
print[data_total]
print[f"Done in {time.time[]-start} seconds"]
4 từ Python để thực hiện việc chia. Một bước khác để thêm sẽ là xóa các tệp đã chia sau khi thực tế

Giải pháp này có khả năng mở rộng nhất và tuân theo kiểu tiếp cận thu nhỏ bản đồ

Thời gian. 3. 25 giây [Người chiến thắng]

import csv
from multiprocessing import Pool, cpu_count
from functools import partial
import os
import time
import subprocess
start = time.time[]

FILE_PATH = "/mnt/c/data/huge_data.csv"
FILE_DIR = "/mnt/c/data"

# First split huge file into multiple
num_cores = cpu_count[]
subprocess.call[["split", "--lines=1000000", "--numeric-suffixes", FILE_PATH, "split"], cwd=FILE_DIR]

def read_data[filename, column_index, skip_header=True]:
    with open[filename] as csv_file:
        if skip_header or filename.endswith['00']:
            next[csv_file]
        for row in csv_file:
            try:
                r = row.split[","]
                yield int[r[column_index]]
            except ValueError:
                print[f"Failed to convert {row[column_index]} to int"]

def sum_file[file_name, column_index, skip_header]:
    return sum[read_data[file_name, column_index, skip_header]]

all_files = os.listdir[FILE_DIR]
file_names = list[filter[lambda name: name.startswith['split'], all_files]] 
file_paths = map[lambda name: os.path.join[FILE_DIR, name], file_names]
with Pool[processes=num_cores] as pool:
    partial_sums = pool.map[partial[sum_file, column_index=2, skip_header=False], file_paths]
    print[f"Total: {sum[partial_sums]}"]
print[f"Done in {time.time[]-start} seconds"]

Sự kết luận

Giống như bất kỳ công cụ nào, thường không có một giải pháp nào luôn tốt nhất

Nếu bạn muốn trăn thuần túy thì cách tiếp cận đầu tiên này là khả thi. Nếu bạn muốn sự đơn giản và đã sử dụng gấu trúc, hãy sử dụng phương pháp gấu trúc. Nếu bạn có tập dữ liệu thực sự lớn, hãy thử đa xử lý với giải pháp tách tệp

Cách nhanh nhất để đọc tệp CSV bằng Python là gì?

Được đo hoàn toàn bằng CPU, fastparquet cho đến nay là nhanh nhất. Việc nó có mang lại cho bạn sự cải thiện về thời gian đã trôi qua hay không sẽ phụ thuộc vào việc bạn có tính năng song song hiện có hay không, máy tính cụ thể của bạn, v.v. Và các tệp CSV khác nhau có lẽ sẽ có chi phí phân tích cú pháp khác nhau; .

Làm cách nào để đọc dữ liệu lớn trong Python?

3 cách xử lý tập dữ liệu lớn trong Python. Là một nhà khoa học dữ liệu, tôi thấy mình ngày càng phải đối mặt với “dữ liệu lớn”. .
Giảm mức sử dụng bộ nhớ bằng cách tối ưu hóa các loại dữ liệu. .
Chia dữ liệu thành nhiều khối. .
Tận dụng đánh giá lười biếng

Chủ Đề