mcpField
S
sn-da-large-file-analysis

sn-da-large-file-analysis

github.com/opensensenova数据与数据库6.0k131 次安装2 天前更新
代码仓库

关于 sn-da-large-file-analysis

sn-da-large-file-analysis 为大型 Excel 数据分析提供按行数选择读取、转换和写入方式的技能,面向处理大表格的数据工作。它涵盖流式读取、Parquet 转换、内存优化和大型文件输出。

主要功能

  1. 01按数据规模选择读取策略
  2. 02流式读取并转换为 Parquet
  3. 03优化数据类型以减少内存占用
  4. 04使用向量化操作处理数据
  5. 05按行数拆分或导出大型文件

适用场景

  1. 01分析 10 万行以上的 Excel 文件
  2. 02将中大型工作簿转换为 Parquet
  3. 03筛选大型交易表并输出结果

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/opensensenova/sensenova-skills --skill sn-da-large-file-analysis

安装后在 Claude Code 中输入/sn-da-large-file-analysis确认已加载。

详细介绍

大规模 Excel 分析技能

强制规则

当总行数 >= 10,000 时,必须使用本技能中的方法。

数据规模读取策略原因
< 10k 行直接使用 pd.read_excel()不会造成内存压力
10k–100k 行pd.read_excel() → 转换为 Parquet → 使用 pd.read_parquet() 进行分析避免重复读取导致速度缓慢
100k–1M 行使用 openpyxl read_only + iter_rows 流式读取 → Parquetpd.read_excel() 会导致内存溢出或超时
> 1M 行流式读取 + 拆分到多个工作表(Excel 每个工作表最多 1,048,576 行)必须分块处理

禁止事项:

  • 对于 100k 行以上的文件,不要使用 pd.read_excel() 完整加载
  • 不要使用 fc-list、find ... fonts 搜索字体,也不要使用 pip install 安装软件包
  • 不要对大型 DataFrame 使用 df.iterrows()(请使用 itertuples() 或向量化操作)
  • 对可以向量化的操作,不要使用 df.apply(lambda...)

环境设置

import pandas as pd
import numpy as np
import os
import gc

pd.options.mode.copy_on_write = True

# CJK font setup (fixed paths — do NOT search for fonts)
# ⚠️ Copy this block as-is. Do NOT use fc-list, find, subprocess, or glob to locate fonts.
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

_FONT_PATHS = [
    '/mnt/afs_agents/SimHei.ttf',
    '/mnt/afs_agents/mnt/data/SimHei.ttf',
    os.path.expanduser('~/.fonts/SimHei.ttf'),
    '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc',
    '/usr/share/fonts/SimHei.ttf',
]
for _p in _FONT_PATHS:
    if os.path.exists(_p):
        fm.fontManager.addfont(_p)
        matplotlib.rcParams['font.family'] = fm.FontProperties(fname=_p).get_name()
        break
matplotlib.rcParams['axes.unicode_minus'] = False

核心方法 1:检查文件结构(不加载数据)

对大型文件进行任何操作之前,先在不将数据加载到内存中的情况下检查工作表和行数:

import openpyxl

def inspect_excel(file_path):
    """Stream-inspect Excel structure. Returns {sheet_name: {rows, columns}}."""
    wb = openpyxl.load_workbook(file_path, read_only=True, data_only=True)
    info = {}
    for name in wb.sheetnames:
        ws = wb[name]
        row_count = 0
        header = None
        for i, row in enumerate(ws.iter_rows(values_only=True)):
            if i == 0:
                header = [str(c) if c is not None else f"Col_{j}" for j, c in enumerate(row)]
            else:
                row_count += 1
        info[name] = {"rows": row_count, "columns": header}
    wb.close()
    return info

# Usage
file_info = inspect_excel(file_path)
for sheet, meta in file_info.items():
    print(f"Sheet '{sheet}': {meta['rows']} rows, {len(meta['columns'])} cols")
    print(f"  Columns: {meta['columns'][:10]}...")
total_rows = sum(m['rows'] for m in file_info.values())
print(f"Total rows: {total_rows}")

核心方法 2:流式读取 → Parquet(100k 行以上)

对于 100k 行以上的文件,切勿使用 pd.read_excel()。请使用 openpyxl 流式读取 → Parquet:

import openpyxl
import pyarrow as pa
import pyarrow.parquet as pq

def stream_excel_to_parquet(excel_path, parquet_path, sheet_name=None, chunk_size=50000):
    """Stream Excel rows to Parquet with constant memory usage.

    All columns are cast to string to avoid cross-chunk schema mismatches
    (Excel mixed-type columns may be all-None in some chunks, causing PyArrow
    to infer null type instead of string). Convert numeric columns after loading
    Parquet with pd.to_numeric() as needed.
    """
    wb = openpyxl.load_workbook(excel_path, read_only=True, data_only=True)
    ws = wb[sheet_name] if sheet_name else wb.active

    header = None
    writer = None
    chunk_rows = []
    total_written = 0

    def _flush(rows):
        nonlocal writer
        table = pa.table({
            col: pa.array(
                [str(r[idx]) if r[idx] is not None else None for r in rows],
                type=pa.string(),
            )
            for idx, col in enumerate(header)
        })
        if writer is None:
            writer = pq.ParquetWriter(parquet_path, table.schema)
        writer.write_table(table)

    for i, row in enumerate(ws.iter_rows(values_only=True)):
        if i == 0:
            header = [str(c) if c is not None else f"Col_{j}" for j, c in enumerate(row)]
            continue

        chunk_rows.append(list(row))

        if len(chunk_rows) >= chunk_size:
            _flush(chunk_rows)
            total_written += len(chunk_rows)
            print(f"  Written {total_written:,} rows...")
            chunk_rows = []
            gc.collect()

    if chunk_rows:
        _flush(chunk_rows)
        total_written += len(chunk_rows)

    if writer:
        writer.close()
    wb.close()
    print(f"Done: {total_written:,} rows -> {parquet_path}")
    return total_written

核心方法 3:中型文件转换为 Parquet(10k–100k 行)

对于 10k–100k 行,pd.read_excel() 不会导致内存溢出,但 Parquet 能大幅提升重复分析的速度:

def convert_excel_to_parquet(excel_path, parquet_path, sheet_name=0):
    """Medium file: pd.read_excel -> Parquet cache."""
    if os.path.exists(parquet_path):
        print(f"Cache exists: {parquet_path}")
        return
    df = pd.read_excel(excel_path, sheet_name=sheet_name)
    df.columns = df.columns.astype(str)
    df.to_parquet(parquet_path, engine='pyarrow', compression='snappy')
    row_count = len(df)
    del df
    gc.collect()
    print(f"Converted {row_count:,} rows -> {parquet_path}")

核心方法 4:内存优化(类型降级)

加载 Parquet 后,进一步减小内存占用:

def optimize_dtypes(df):
    """Auto-downcast numeric types + convert low-cardinality strings to Category.
    Typically saves 50-80% memory."""
    start_mb = df.memory_usage(deep=True).sum() / 1024**2

    for col in df.select_dtypes(include=['int64', 'int32']).columns:
        c_min, c_max = df[col].min(), df[col].max()
        if c_min >= np.iinfo(np.int8).min and c_max <= np.iinfo(np.int8).max:
            df[col] = df[col].astype(np.int8)
        elif c_min >= np.iinfo(np.int16).min and c_max <= np.iinfo(np.int16).max:
            df[col] = df[col].astype(np.int16)
        elif c_min >= np.iinfo(np.int32).min and c_max <= np.iinfo(np.int32).max:
            df[col] = df[col].astype(np.int32)

    for col in df.select_dtypes(include=['float64']).columns:
        df[col] = df[col].astype(np.float32)

    for col in df.select_dtypes(include=['object', 'string']).columns:
        if df[col].nunique() / max(len(df), 1) < 0.5:
            df[col] = df[col].astype('category')

    end_mb = df.memory_usage(deep=True).sum() / 1024**2
    print(f"Memory: {start_mb:.1f} MB -> {end_mb:.1f} MB (saved {(1 - end_mb/start_mb)*100:.0f}%)")
    return df

核心方法 5:写入大型文件

def write_large_excel(df, output_path, sheet_name="Sheet1"):
    """Auto-select write strategy based on data size."""
    total_cells = len(df) * len(df.columns)

    if len(df) > 1_000_000:
        csv_path = output_path.rsplit('.', 1)[0] + '.csv'
        df.to_csv(csv_path, index=False)
        print(f"Over 1M rows — exported as CSV: {csv_path}")
        return csv_path

    if total_cells > 50_000:
        from openpyxl import Workbook
        from openpyxl.cell import WriteOnlyCell

        wb = Workbook(write_only=True)
        ws = wb.create_sheet(title=sheet_name)
        ws.append(list(df.columns))
        for idx, row in enumerate(df.itertuples(index=False)):
            ws.append([None if pd.isna(v) else v for v in row])
            if (idx + 1) % 100_000 == 0:
                print(f"  Written {idx + 1:,} rows...")
        wb.save(output_path)
        wb.close()
        print(f"write_only mode: {len(df):,} rows -> {output_path}")
    else:
        df.to_excel(output_path, index=False, sheet_name=sheet_name)
        print(f"Standard write: {len(df):,} rows -> {output_path}")
    return output_path

示例 1:10 万行表格 — 列分布 + 图表

场景:用户有一个包含 10 万行数据的销售 Excel 文件,需要查看各地区的销售分布并绘制柱状图。

import pandas as pd
import os, gc

excel_path = "sales_100k.xlsx"
parquet_path = "sales_100k.parquet"

# === Step 1: Inspect structure ===
file_info = inspect_excel(excel_path)
total_rows = sum(m['rows'] for m in file_info.values())
print(f"Total rows: {total_rows}")

# === Step 2: Choose read strategy by row count ===
if total_rows >= 100_000:
    stream_excel_to_parquet(excel_path, parquet_path)
else:
    convert_excel_to_parquet(excel_path, parquet_path)

# === Step 3: Load Parquet + optimize memory ===
df = pd.read_parquet(parquet_path)
df = optimize_dtypes(df)
print(f"Shape: {df.shape}")
print(df.head(3))

# === Step 4: Analysis ===
region_sales = df.groupby('Region')['Sales'].sum().sort_values(ascending=False)
print(region_sales)

# === Step 5: Visualization ===
fig, ax = plt.subplots(figsize=(10, 6))
region_sales.plot(kind='bar', ax=ax, color='#4C72B0')
ax.set_title('Sales by Region')
ax.set_ylabel('Sales')
plt.tight_layout()
plt.savefig('region_sales.png', dpi=150, bbox_inches='tight')
plt.show()

# === Step 6: Cleanup ===
del df
gc.collect()

示例 2:100 万行表格 — 流式读取 + 筛选 + 导出

场景:用户有一份包含 100 万行的交易日志,需要筛选金额 > 10,000 的记录并导出。

import pandas as pd
import os, gc

excel_path = "transactions_1m.xlsx"
parquet_path = "transactions_1m.parquet"

# === Step 1: Stream to Parquet (1M rows — MUST use streaming, never pd.read_excel) ===
stream_excel_to_parquet(excel_path, parquet_path, chunk_size=50000)

# === Step 2: Load only needed columns (saves memory) ===
df = pd.read_parquet(parquet_path, columns=['TransactionID', 'Amount', 'Date', 'Type'])
df = optimize_dtypes(df)
print(f"Shape: {df.shape}, Memory: {df.memory_usage(deep=True).sum()/1024**2:.1f} MB")

# === Step 3: Vectorized filtering (never use apply/iterrows) ===
mask = df['Amount'] > 10000
high_value = df[mask].copy()
print(f"Filtered: {len(high_value):,} / {len(df):,} rows")

# === Step 4: Export ===
output_path = write_large_excel(high_value, 'high_value_transactions.xlsx')

# === Step 5: Cleanup ===
del df, high_value
gc.collect()

向量化操作速查表

处理大型文件时,切勿使用低效操作——请改用向量化替代方案:

低效(禁止)高效(请使用)
df.apply(lambda x: x*2)df['col'] * 2
df.iterrows()df.itertuples(index=False)
for i in range(len(df)): df.iloc[i]向量化布尔索引 df[mask]
df['a'].map(lambda x: 'Y' if x>0 else 'N')np.where(df['a']>0, 'Y', 'N')
df.groupby('a').apply(custom_func)df.groupby('a').agg({'b':'sum','c':'mean'})

内存估算

加载数据前先估算内存,避免内存溢出:

Estimated MB ≈ rows × cols × 8 / 1024² (numeric columns)
Estimated MB ≈ rows × cols × 50 / 1024² (with text columns)
行数20 列(数值型)20 列(含文本)
100k~15 MB~95 MB
500k~76 MB~477 MB
1M~153 MB~953 MB

当估算内存超过可用 RAM 的 80% 时,请使用按列选择加载(pd.read_parquet(columns=[...]))或分块处理。


最佳实践

  1. Parquet 是首选:对于任何行数 >= 10k 的文件,分析前都应转换为 Parquet。Parquet 支持按列读取和压缩存储,加载速度比 xlsx 快 10–50 倍。
  2. 流式读取是安全保障:对于 100k 行以上的数据,始终使用 openpyxl read_only + iter_rows。切勿使用 pd.read_excel() 完整加载。
  3. 及时释放内存:每处理完一个中间 DataFrame,都执行 del df; gc.collect()。
  4. Excel 行数限制:每个工作表最多 1,048,576 行。超过限制时,自动拆分到多个工作表或导出为 CSV。
  5. 输出时使用 write_only:超过 50k 个单元格的文件必须使用 openpyxl Workbook(write_only=True)。

评论

支持 Markdown