# Categorical Comparison Analysis

Compares two categorical datasets by count, ratio and difference, with charts.

- Repository: https://github.com/OpenSenseNova/SenseNova-Skills (6002 stars)
- Category: Data and ML
- License: MIT
- Last updated: 2026-10-29
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add OpenSenseNova/SenseNova-Skills --skill categorical-comparison-analysis -g -a claude-code -y`
Codex: `npx skills add OpenSenseNova/SenseNova-Skills --skill categorical-comparison-analysis -g -a codex -y`
Cursor: `npx skills add OpenSenseNova/SenseNova-Skills --skill categorical-comparison-analysis -g -a cursor -y`
Gemini CLI: `npx skills add OpenSenseNova/SenseNova-Skills --skill categorical-comparison-analysis -g -a gemini-cli -y`

## Description

对两类分类数据进行对比分析，统计数量差异与比例关系并生成可视化图表。

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# categorical-comparison-analysis

> This sub-skill covers one capability of the Excel workflow. For reading/counting/Parquet optimization, see the parent workflow SKILL.md.

Step1 读取文件并统计所有 sheet 的总行数，评估是否需要进行大文件优化处理。
```python
import pandas as pd
from pandas import read_excel
from pathlib import Path

# 统计所有 sheet 的行数以决定处理策略
file_path = "input_data.xlsx"
sheet_names = pd.ExcelFile(file_path).sheet_names
total_rows = 0
for sheet in sheet_names:
    # 仅读取行索引以快速计数
    df_tmp = read_excel(file_path, sheet_name=sheet, usecols=[0])
    total_rows += len(df_tmp)

print(f"Total rows across all sheets: {total_rows}")
```

Step2 提取对比维度的分类信息，执行数据清洗，包括去除空值、处理合并单元格填充以及排除非数据行。
```python
# 定义目标列名
target_col_a = "category_a_column"
target_col_b = "category_b_column"

# 处理合并单元格（ffill）并清洗数据
df[target_col_a] = df[target_col_a].ffill()
df[target_col_b] = df[target_col_b].ffill()

# 排除标题行占位符（如 '代码'、'名称'）及空值
exclude_val = "代码" 
data_a = df[target_col_a].dropna()
data_a = data_a[data_a != exclude_val]

data_b = df[target_col_b].dropna()
data_b = data_b[data_b != exclude_val]
```

Step3 统计分类数量，计算差异值与占比，生成多维度对比统计表。
```python
count_a = len(data_a)
count_b = len(data_b)
total_count = count_a + count_b
difference = abs(count_a - count_b)

# 计算占比
ratio_a = (count_a / total_count) * 100 if total_count > 0 else 0
ratio_b = (count_b / total_count) * 100 if total_count > 0 else 0

# 构建统计摘要
summary_df = pd.DataFrame({
    "分类名称": ["类别A", "类别B"],
    "数量": [count_a, count_b],
    "占比": [f"{ratio_a:.2f}%", f"{ratio_b:.2f}%"]
})
print(summary_df)
print(f"数量差异: {difference}")
```

Step4 配置中文字体并生成可视化图表（柱状图与饼图），美化输出效果。
```python
import matplotlib.pyplot as plt

# 中文字体配置
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
labels = ['类别A', '类别B']
counts = [count_a, count_b]
colors = ['#3498db', '#e74c3c']

The rest: https://github.com/OpenSenseNova/SenseNova-Skills/blob/HEAD/skills/sn-da-excel-workflow/capability/excel-data-analysis/comparison-analysis/SKILL.md
