Pandas Pro

作者 jeffallan1be15d8064f8MIT11K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5天前更新

Performs pandas DataFrame operations for data analysis, manipulation, and transformation. Use when working with pandas DataFrames, data cleaning, aggregation, merging, or time series analysis. Invoke for data manipulation tasks such as joining DataFrames on multiple keys, pivoting tables, resampling time series, handling NaN values with interpolation or forward-fill, groupby aggregations, type conversion, or performance optimization of large datasets.

仅含说明Data & Analytics
AI 生成的概览

指导 pandas DataFrame 操作、清洗、聚合、合并与性能优化,并附带参考文件。

功能
提供 pandas 数据处理、分析与转换方面的专业指导。涵盖 DataFrame 操作、数据清洗、groupby 聚合、合并与连接、时间序列重采样、透视表以及内存优化。产出代码模式、验证检查与性能考量,而非文件。
适用场景
适用于使用 pandas DataFrame 进行数据清洗、聚合、合并、透视或时间序列分析的场景。也适合优化大型数据集的内存与性能。
运行要求
不附带脚本或软件包,仅为说明文档。代理需要在工作环境中安装 pandas 才能运行示例代码。

Pandas Pro

Expert pandas developer specializing in efficient data manipulation, analysis, and transformation workflows with production-grade performance patterns.

Core Workflow

  1. Assess data structure — Examine dtypes, memory usage, missing values, data quality:
    python
    print(df.dtypes)print(df.memory_usage(deep=True).sum() / 1e6, "MB")print(df.isna().sum())print(df.describe(include="all"))
  2. Design transformation — Plan vectorized operations, avoid loops, identify indexing strategy
  3. Implement efficiently — Use vectorized methods, method chaining, proper indexing
  4. Validate results — Check dtypes, shapes, null counts, and row counts:
    python
    assert result.shape[0] == expected_rows, f"Row count mismatch: {result.shape[0]}"assert result.isna().sum().sum() == 0, "Unexpected nulls after transform"assert set(result.columns) == expected_cols
  5. Optimize — Profile memory, apply categorical types, use chunking if needed

Reference Guide

Load detailed guidance based on context:

TopicReferenceLoad When
DataFrame Operationsreferences/dataframe-operations.mdIndexing, selection, filtering, sorting
Data Cleaningreferences/data-cleaning.mdMissing values, duplicates, type conversion
Aggregation & GroupByreferences/aggregation-groupby.mdGroupBy, pivot, crosstab, aggregation
Merging & Joiningreferences/merging-joining.mdMerge, join, concat, combine strategies
Performance Optimizationreferences/performance-optimization.mdMemory usage, vectorization, chunking

Code Patterns

Vectorized Operations (before/after)

python
# ❌ AVOID: row-by-row iterationfor i, row in df.iterrows():    df.at[i, 'tax'] = row['price'] * 0.2
# ✅ USE: vectorized assignmentdf['tax'] = df['price'] * 0.2

Safe Subsetting with .copy()

python
# ❌ AVOID: chained indexing triggers SettingWithCopyWarningdf['A']['B'] = 1
# ✅ USE: .loc[] with explicit copy when mutating a subsetsubset = df.loc[df['status'] == 'active', :].copy()subset['score'] = subset['score'].fillna(0)

GroupBy Aggregation

python
summary = (    df.groupby(['region', 'category'], observed=True)    .agg(        total_sales=('revenue', 'sum'),        avg_price=('price', 'mean'),        order_count=('order_id', 'nunique'),    )    .reset_index())

Merge with Validation

python
merged = pd.merge(    left_df, right_df,    on=['customer_id', 'date'],    how='left',    validate='m:1',          # asserts right key is unique    indicator=True,)unmatched = merged[merged['_merge'] != 'both']print(f"Unmatched rows: {len(unmatched)}")merged.drop(columns=['_merge'], inplace=True)

Missing Value Handling

python
# Forward-fill then interpolate numeric gapsdf['price'] = df['price'].ffill().interpolate(method='linear')
# Fill categoricals with mode, numerics with medianfor col in df.select_dtypes(include='object'):    df[col] = df[col].fillna(df[col].mode()[0])for col in df.select_dtypes(include='number'):    df[col] = df[col].fillna(df[col].median())

Time Series Resampling

python
daily = (    df.set_index('timestamp')    .resample('D')    .agg({'revenue': 'sum', 'sessions': 'count'})    .fillna(0))

Pivot Table

python
pivot = df.pivot_table(    values='revenue',    index='region',    columns='product_line',    aggfunc='sum',    fill_value=0,    margins=True,)

Memory Optimization

python
# Downcast numerics and convert low-cardinality strings to categoricaldf['category'] = df['category'].astype('category')df['count'] = pd.to_numeric(df['count'], downcast='integer')df['score'] = pd.to_numeric(df['score'], downcast='float')print(df.memory_usage(deep=True).sum() / 1e6, "MB after optimization")

Constraints

MUST DO

  • Use vectorized operations instead of loops
  • Set appropriate dtypes (categorical for low-cardinality strings)
  • Check memory usage with .memory_usage(deep=True)
  • Handle missing values explicitly (don't silently drop)
  • Use method chaining for readability
  • Preserve index integrity through operations
  • Validate data quality before and after transformations
  • Use .copy() when modifying subsets to avoid SettingWithCopyWarning

MUST NOT DO

  • Iterate over DataFrame rows with .iterrows() unless absolutely necessary
  • Use chained indexing (df['A']['B']) — use .loc[] or .iloc[]
  • Ignore SettingWithCopyWarning messages
  • Load entire large datasets without chunking
  • Use deprecated methods (.ix, .append() — use pd.concat())
  • Convert to Python lists for operations possible in pandas
  • Assume data is clean without validation

Output Templates

When implementing pandas solutions, provide:

  1. Code with vectorized operations and proper indexing
  2. Comments explaining complex transformations
  3. Memory/performance considerations if dataset is large
  4. Data validation checks (dtypes, nulls, shapes)

Maintained by @jeffallan, Principal Consultant at Synergetic Solutions

Documentation

来源与署名

来源:jeffallan/claude-skills位于skills/pandas-pro提交1be15d8

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架