invalid-data-cleaning
用于大规模Excel数据的预处理,通过统计总行数判断是否转换为Parquet格式以提升读写效率,并使用正则表达式清洗指定文本列(如仅保留中文字符),最后导出清洗后的文件并提供下载链接。
它会碰到什么
扫了多少1 个文本文件,2 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Invalid_Data_Cleaning
> This sub-skill covers one capability of the Excel workflow. For reading/counting/Parquet optimization, see the parent workflow SKILL.md.
Skill Steps
Step1 根据总行数判断是否数据量过大,若满足条件,则将 Excel 文件转换为 Parquet 格式提升读写效率,再读取数据进行后续分析。
import pandas as pd
file_path = "input_data.xlsx"
parquet_path = "temp_data.parquet"
# 读取 Excel 文件并转换为 Parquet 格式
xls = pd.ExcelFile(file_path)
dfs = []
for sheet in xls.sheet_names:
df_sheet = pd.read_excel(xls, sheet_name=sheet)
dfs.append(df_sheet)
# 合并所有 sheet 数据并写入 Parquet 文件
if dfs:
df_all = pd.concat(dfs, ignore_index=True)
df_all.to_parquet(parquet_path, engine='pyarrow', index=False)
# 读取 Parquet 文件用于后续处理
df = pd.read_parquet(parquet_path)
Step2 对目标文本字段中的特殊字符(如 #、-、数字)进行清洗,使用正则表达式仅保留中文字符。
import pandas as pd
import re
target_col = 'target_column' # 替换为实际需要清洗的列名
# 定义清洗函数
def clean_chinese_text(text):
if pd.isna(text):
return text
s = str(text)
# 提取所有中文字符(Unicode 范围:[一-鿿])
chinese_chars = re.findall(r'[一-鿿]', s)
cleaned = ''.join(chinese_chars)
return cleaned if cleaned else ''
# 应用清洗函数
if target_col in df.columns:
df[target_col] = df[target_col].apply(clean_chinese_text)
Step3 将清洗后的数据保存为表格文件(.xlsx),并在报告中提供本地下载链接。
import pandas as pd
# 保存清洗后的数据为 .xlsx 文件
output_path = "cleaned_data.xlsx"
df.to_excel(output_path, index=False)
print("清洗后的数据已保存至:", output_path)
# 生成本地文件下载链接
print("下载链接:", f"file://{output_path}")想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
星标★ 5,638
本站分层T1
该仓技能数80
原文件路径
skills/sn-da-excel-workflow/capability/excel-data-cleaning/invalid-data-cleaning/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