{"data":{"slug":"github-opensensenova-sensenova-skills-skills-sn-da-excel-workflow-capability-excel-data-analysis-comparison-analysis-ab57d875bd8e","title":"Compare Category Counts and Shares in Excel","summary":"Compare two category groups in Excel by count and percentage share. Export a summary workbook and chart image.","url":"https:\/\/skillshunter.net\/skills\/github-opensensenova-sensenova-skills-skills-sn-da-excel-workflow-capability-excel-data-analysis-comparison-analysis-ab57d875bd8e","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-opensensenova-sensenova-skills-skills-sn-da-excel-workflow-capability-excel-data-analysis-comparison-analysis-ab57d875bd8e","markdown_url":"https:\/\/skillshunter.net\/skills\/github-opensensenova-sensenova-skills-skills-sn-da-excel-workflow-capability-excel-data-analysis-comparison-analysis-ab57d875bd8e.md","download_url":"https:\/\/skillshunter.net\/skills\/github-opensensenova-sensenova-skills-skills-sn-da-excel-workflow-capability-excel-data-analysis-comparison-analysis-ab57d875bd8e\/download","category":{"slug":"data-research","name":"Data & Research"},"editor_note":"The skill involves reading Excel files, cleaning data, calculating counts and percentages, and generating visualizations. It requires Python and libraries like pandas and matplotlib.","instructions":"---\nname: categorical-comparison-analysis\ndescription: \"\u5bf9\u4e24\u7c7b\u5206\u7c7b\u6570\u636e\u8fdb\u884c\u5bf9\u6bd4\u5206\u6790\uff0c\u7edf\u8ba1\u6570\u91cf\u5dee\u5f02\u4e0e\u6bd4\u4f8b\u5173\u7cfb\u5e76\u751f\u6210\u53ef\u89c6\u5316\u56fe\u8868\u3002\"\n---\n\n# categorical-comparison-analysis\n\n> This sub-skill covers one capability of the Excel workflow. For reading\/counting\/Parquet optimization, see the parent workflow SKILL.md.\n\nStep1 \u8bfb\u53d6\u6587\u4ef6\u5e76\u7edf\u8ba1\u6240\u6709 sheet \u7684\u603b\u884c\u6570\uff0c\u8bc4\u4f30\u662f\u5426\u9700\u8981\u8fdb\u884c\u5927\u6587\u4ef6\u4f18\u5316\u5904\u7406\u3002\n```python\nimport pandas as pd\nfrom pandas import read_excel\nfrom pathlib import Path\n\n# \u7edf\u8ba1\u6240\u6709 sheet \u7684\u884c\u6570\u4ee5\u51b3\u5b9a\u5904\u7406\u7b56\u7565\nfile_path = \"input_data.xlsx\"\nsheet_names = pd.ExcelFile(file_path).sheet_names\ntotal_rows = 0\nfor sheet in sheet_names:\n    # \u4ec5\u8bfb\u53d6\u884c\u7d22\u5f15\u4ee5\u5feb\u901f\u8ba1\u6570\n    df_tmp = read_excel(file_path, sheet_name=sheet, usecols=[0])\n    total_rows += len(df_tmp)\n\nprint(f\"Total rows across all sheets: {total_rows}\")\n```\n\nStep2 \u63d0\u53d6\u5bf9\u6bd4\u7ef4\u5ea6\u7684\u5206\u7c7b\u4fe1\u606f\uff0c\u6267\u884c\u6570\u636e\u6e05\u6d17\uff0c\u5305\u62ec\u53bb\u9664\u7a7a\u503c\u3001\u5904\u7406\u5408\u5e76\u5355\u5143\u683c\u586b\u5145\u4ee5\u53ca\u6392\u9664\u975e\u6570\u636e\u884c\u3002\n```python\n# \u5b9a\u4e49\u76ee\u6807\u5217\u540d\ntarget_col_a = \"category_a_column\"\ntarget_col_b = \"category_b_column\"\n\n# \u5904\u7406\u5408\u5e76\u5355\u5143\u683c\uff08ffill\uff09\u5e76\u6e05\u6d17\u6570\u636e\ndf[target_col_a] = df[target_col_a].ffill()\ndf[target_col_b] = df[target_col_b].ffill()\n\n# \u6392\u9664\u6807\u9898\u884c\u5360\u4f4d\u7b26\uff08\u5982 '\u4ee3\u7801'\u3001'\u540d\u79f0'\uff09\u53ca\u7a7a\u503c\nexclude_val = \"\u4ee3\u7801\" \ndata_a = df[target_col_a].dropna()\ndata_a = data_a[data_a != exclude_val]\n\ndata_b = df[target_col_b].dropna()\ndata_b = data_b[data_b != exclude_val]\n```\n\nStep3 \u7edf\u8ba1\u5206\u7c7b\u6570\u91cf\uff0c\u8ba1\u7b97\u5dee\u5f02\u503c\u4e0e\u5360\u6bd4\uff0c\u751f\u6210\u591a\u7ef4\u5ea6\u5bf9\u6bd4\u7edf\u8ba1\u8868\u3002\n```python\ncount_a = len(data_a)\ncount_b = len(data_b)\ntotal_count = count_a + count_b\ndifference = abs(count_a - count_b)\n\n# \u8ba1\u7b97\u5360\u6bd4\nratio_a = (count_a \/ total_count) * 100 if total_count > 0 else 0\nratio_b = (count_b \/ total_count) * 100 if total_count > 0 else 0\n\n# \u6784\u5efa\u7edf\u8ba1\u6458\u8981\nsummary_df = pd.DataFrame({\n    \"\u5206\u7c7b\u540d\u79f0\": [\"\u7c7b\u522bA\", \"\u7c7b\u522bB\"],\n    \"\u6570\u91cf\": [count_a, count_b],\n    \"\u5360\u6bd4\": [f\"{ratio_a:.2f}%\", f\"{ratio_b:.2f}%\"]\n})\nprint(summary_df)\nprint(f\"\u6570\u91cf\u5dee\u5f02: {difference}\")\n```\n\nStep4 \u914d\u7f6e\u4e2d\u6587\u5b57\u4f53\u5e76\u751f\u6210\u53ef\u89c6\u5316\u56fe\u8868\uff08\u67f1\u72b6\u56fe\u4e0e\u997c\u56fe\uff09\uff0c\u7f8e\u5316\u8f93\u51fa\u6548\u679c\u3002\n```python\nimport matplotlib.pyplot as plt\n\n# \u4e2d\u6587\u5b57\u4f53\u914d\u7f6e\nplt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']\nplt.rcParams['axes.unicode_minus'] = False\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))\nlabels = ['\u7c7b\u522bA', '\u7c7b\u522bB']\ncounts = [count_a, count_b]\ncolors = ['#3498db', '#e74c3c']\n\n# \u67f1\u72b6\u56fe\u7f8e\u5316\nbars = ax1.bar(labels, counts, color=colors, alpha=0.8, edgecolor='black')\nax1.set_title('\u5206\u7c7b\u6570\u91cf\u5bf9\u6bd4', fontsize=14)\nax1.grid(axis='y', linestyle='--', alpha=0.6)\nfor bar in bars:\n    height = bar.get_height()\n    ax1.text(bar.get_x() + bar.get_width()\/2., height + 0.1, f'{int(height)}', \n             ha='center', va='bottom', fontweight='bold')\n\n# \u997c\u56fe\u7f8e\u5316\nax2.pie(counts, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140, explode=(0.05, 0))\nax2.set_title('\u5206\u7c7b\u6bd4\u4f8b\u5206\u5e03', fontsize=14)\n\noutput_img = \"\/mnt\/data\/comparison_analysis_chart.png\"\nplt.tight_layout()\nplt.savefig(output_img, dpi=300, bbox_inches='tight')\nplt.show()\n```\n\nStep5 \u5c06\u5206\u6790\u7ed3\u679c\u5bfc\u51fa\u4e3a Excel \u6587\u4ef6\uff0c\u5e76\u751f\u6210\u53ef\u4f9b\u4e0b\u8f7d\u7684\u94fe\u63a5\u3002\n```python\nfrom IPython.display import FileLink\n\noutput_path = \"\/mnt\/data\/analysis_report.xlsx\"\nwith pd.ExcelWriter(output_path) as writer:\n    summary_df.to_excel(writer, sheet_name='\u7edf\u8ba1\u6458\u8981', index=False)\n    # \u5982\u679c\u6709\u660e\u7ec6\u6570\u636e\u4e5f\u53ef\u5728\u6b64\u5bfc\u51fa\n\nprint(f\"\u5206\u6790\u62a5\u544a\u5df2\u751f\u6210\")\ndisplay(FileLink(output_path, result_html_prefix=\"\u4e0b\u8f7d\u5206\u6790\u62a5\u544a: \"))\n```\n","use_cases":["Compare counts for two product categories.","Report percentage shares for two customer groups."],"limitations":["Requires two identifiable category columns.","Counts retained rows. Deduplicate first if unique entities are required."],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/OpenSenseNova\/SenseNova-Skills\/blob\/07e7ff8c4bdfd7b79e7402fe94af59d2a5a9749b\/skills\/sn-da-excel-workflow\/capability\/excel-data-analysis\/comparison-analysis\/SKILL.md","source_commit":"07e7ff8c4bdfd7b79e7402fe94af59d2a5a9749b","license":null,"version":null,"listing_approved_at":"2026-11-15","created_at":"2026-11-14T13:51:08.000000Z","updated_at":"2026-11-15T16:31:33.000000Z","seo_title":"Compare Category Counts and Shares in Excel","seo_description":"Compare two category groups in Excel. Calculate counts, differences, and percentage shares, then export a summary workbook and charts.","seo_keywords":["Excel category comparison","percentage share analysis","Excel comparison charts"]}}