You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

内存不足时读取大型CSV/数据库并关联列的解决方案(含Medicare案例)

Answer

Absolutely! You don’t need to load the entire 750MB CSV into memory to extract just the NPI, LastName, and FirstName columns and join them with your summary_df. Here are a few practical, memory-efficient approaches tailored to your use case:

1. Pandas Chunking (Simple, No Extra Libraries)

Pandas lets you read the CSV in smaller, memory-friendly chunks, filter each chunk to keep only the columns you need, then combine the filtered chunks into a lightweight dataframe for joining.

import pandas as pd

# Define the exact columns we need to keep
target_cols = ['NPI', 'LastName', 'FirstName']

# Initialize a list to store filtered chunks
filtered_chunks = []

# Read the CSV in chunks (adjust chunk size based on your available memory)
for chunk in pd.read_csv('physician_df.csv', usecols=target_cols, chunksize=100000):
    # Drop rows with missing NPI (since we need this key for joining)
    chunk = chunk.dropna(subset=['NPI'])
    filtered_chunks.append(chunk)

# Combine chunks into a single small dataframe
physician_small = pd.concat(filtered_chunks, ignore_index=True)

# Perform the join with your preloaded summary_df
merged_df = pd.merge(summary_df, physician_small, on='NPI', how='left')

The usecols parameter is critical here—it tells pandas to skip loading all 38 unnecessary columns entirely, cutting down on memory usage before chunking even starts.

2. Dask (For Seamless Out-of-Core Processing)

If you anticipate working with even larger datasets down the line, Dask is built for handling data bigger than your available memory. It mimics pandas syntax but manages chunking and processing in the background.

import dask.dataframe as dd

# Read only the columns we care about
physician_dask = dd.read_csv('physician_df.csv', usecols=['NPI', 'LastName', 'FirstName'])

# Clean up missing NPI values early
physician_dask = physician_dask.dropna(subset=['NPI'])

# Convert your in-memory summary_df to a Dask dataframe (or keep it as pandas—Dask handles both)
summary_dask = dd.from_pandas(summary_df, npartitions=2)

# Run the join operation
merged_dask = dd.merge(summary_dask, physician_dask, on='NPI', how='left')

# Bring the final merged result into memory (only the joined data, not the full physician dataset)
merged_df = merged_dask.compute()

3. Preprocess with Command-Line Tools (Optional)

If you want to create a permanently reduced CSV file for future use, tools like csvkit or awk can extract just your target columns in seconds:

Using csvkit:

csvcut -c NPI,LastName,FirstName physician_df.csv > physician_small.csv

Then load the tiny physician_small.csv into pandas normally—no chunking required.

Quick Tips:

  • Use how='left' in the merge to retain all rows from summary_df (switch to inner if you only want rows with matching NPIs).
  • Always drop rows with missing join keys early to avoid wasted memory and join errors.

内容的提问来源于stack exchange,提问作者CandleWax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:07:27