dynamic-percentage-and-large-file-analysis
根据文件行数动态切换大文件处理策略(Parquet转换),通过逐行扫描或列匹配提取关键指标并计算占比、均值等统计量,最终输出结构化Excel报告及可视化图表。
它会碰到什么
扫了多少1 个文本文件,3 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Skill Steps
> This sub-skill covers one capability of the Excel workflow. For reading/counting/Parquet optimization, see the parent workflow SKILL.md.
Step1 在数据中动态定位关键字段,通过逐行扫描匹配关键词提取数值,并进行条件筛选与占比计算。
key_values = {}
target_col = None
value_col = 'target_value_col'
# 动态查找目标分类列
for col in df_analysis.columns:
if 'keyword1' in col.lower() or 'keyword2' in col.lower():
target_col = col
break
# 通用字段查找逻辑:逐行扫描匹配关键词并提取首个正数
for idx, row in df_analysis.iterrows():
row_str = str(row.values)
if '指标A' in row_str and '指标A' not in key_values:
for val in row.values:
if isinstance(val, (int, float)) and val > 0:
key_values['指标A'] = val
break
if '指标B' in row_str and '指标B' not in key_values:
for val in row.values:
if isinstance(val, (int, float)) and val > 0:
key_values['指标B'] = val
break
# 条件筛选与统计
if target_col and '特定类别' in df_analysis[target_col].unique():
df_filtered = df_analysis[df_analysis[target_col] == '特定类别']
if value_col in df_filtered.columns:
df_filtered[value_col] = pd.to_numeric(df_filtered[value_col], errors='coerce')
avg_val = df_filtered[value_col].mean()
print(f"特定类别平均值 = {avg_val:.2f}")
# 计算占比
if '指标A' in key_values and '指标B' in key_values:
percentage = (key_values['指标A'] / key_values['指标B']) * 100
print(f"指标A占指标B的百分比: {percentage:.2f}%")
Step2 将计算结果保存为结构化表格文件(.xlsx),并在输出中提供可追溯的下载链接。
output_path = "output_analysis_result.xlsx"
os.makedirs(os.path.dirname(output_path), exist_ok=True)
result_data = {
'项目': ['指标A', '指标B', '占比'],
'数值': [key_values.get('指标A', 0), key_values.get('指标B', 0), f"{percentage:.2f}%" if 'percentage' in locals() else "N/A"]
}
df_result = pd.DataFrame(result_data)
with pd.ExcelWriter(output_path, engine='openpyxl') as writer:
df_result.to_excel(writer, sheet_name='汇总结果', index=False)
print(f"结果已保存到: {output_path}")
print(f"下载链接: [点击下载结果表格]({output_path})")
Step3 配置中文字体并生成高分辨率的可视化图表(如饼图),展示占比分析结果。
import matplotlib.pyplot as plt
import matplotlib
# 配置中英文字体,防止图表中文乱码
matplotlib.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'WenQuanYi Zen Hei']
matplotlib.rcParams['axes.unicode_minus'] = False
if 'percentage' in locals():
# 图表美化与高分辨率设置
plt.figure(figsize=(8, 6), dpi=120)
labels = ['指标A', '其他']
sizes = [percentage, 100 - percentage]
colors = ['#ff9999', '#66b3ff']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
plt.title('核心指标占比分析')
plt.axis('equal')
chart_path = "percentage_chart.png"
plt.savefig(chart_path, bbox_inches='tight')
print(f"图表已保存至: {chart_path}")
print(f"图表下载链接: [点击下载可视化图表]({chart_path})")想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
星标★ 5,638
本站分层T1
该仓技能数80
原文件路径
skills/sn-da-excel-workflow/capability/excel-data-statistics/percentage-calculation/SKILL.md同一个仓库里的其他技能
- large-file-parquet-analysis-and-highlight
- excel-conditional-comparison-and-large-file-processing
- excel-outlier-detection-and-highlighting
- large-file-conditional-formatting
- top-value-coloring
- numeric-extraction-and-distribution-analysis
- categorical-comparison-analysis
- group-by-analysis
- large-file-kpi-analysis
- pivot-table-cross-analysis
- time-series-and-categorical-analysis
- trend-analysis