1. 安装
使用 pip 或 conda 安装:
# 使用 pippip install ydata-profiling# 或者使用 condaconda install -c conda-forge ydata-profiling
2. 基本使用:一行代码生成完整报告
最常见的用法是加载数据后,直接创建 ProfileReport 对象并保存为 HTML 文件。
import pandas as pdfrom ydata_profiling import ProfileReport# 加载示例数据(这里使用 pandas 自带的泰坦尼克数据集)df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")# 生成报告profile = ProfileReport(df, title="Titanic Dataset Profiling Report")# 保存为 HTML 文件profile.to_file("titanic_report.html")
如果你在 Jupyter Notebook 中,还可以直接在单元格内显示报告:
# 在 Jupyter 中内嵌显示报告profile.to_notebook_iframe()
3. 自定义配置:精细化控制报告内容
YData-Profiling 允许通过 ProfileReport 的 config 参数传入一个配置字典,或者使用 config 模块来精细控制报告的各个方面(如关闭某些图表、调整阈值、指定要分析的变量等)。
from ydata_profiling import ProfileReportimport pandas as pddf = pd.read_csv("titanic.csv")# 自定义配置config = {"title": "自定义标题","dataset": {"description": "这是一个泰坦尼克乘客数据集的详细分析","copyright_holder": "数据科学团队","url": "https://example.com"},"variables": {"descriptions": {"Age": "乘客年龄(岁)","Fare": "船票价格(英镑)"}},"correlations": {"pearson": {"calculate": True},"spearman": {"calculate": True},"kendall": {"calculate": False},"cramers": {"calculate": True}},"missing_diagrams": {"bar": True, "matrix": True, "heatmap": True},"interactions": {"continuous": False}, # 关闭连续变量的交互图"plot": {"histogram": {"bins": 50}} # 调整直方图的 bin 数}profile = ProfileReport(df, config=config)profile.to_file("custom_report.html")
4. 最小模式:加速大型数据集的分析
当数据集非常大(例如百万行以上)时,可以开启 minimal=True 模式,它会跳过一些计算密集型图表(如交互图、相关性热图等),只生成核心统计信息,速度会大幅提升。
profile_minimal = ProfileReport(df, title="Minimal Report", minimal=True)profile_minimal.to_file("minimal_report.html")
5. 时间序列模式:自动识别时间列并生成时序分析
如果你的数据包含日期/时间列,可以通过设置 tsmode=True 和 sortby 参数开启时间序列模式,报告将额外包含自相关图、偏自相关图、平稳性检验等。
# 构造一个简单的时间序列数据date_rng = pd.date_range(start='2020-01-01', end='2022-12-31', freq='D')ts_df = pd.DataFrame(date_rng, columns=['date'])ts_df['value'] = np.random.randn(len(date_rng)).cumsum() # 随机游走# 开启时间序列模式profile_ts = ProfileReport(ts_df, title="Time Series Report",tsmode=True, sortby="date")profile_ts.to_file("time_series_report.html")
6. 数据集对比:比较两个 DataFrame 的差异
通过 compare 方法,你可以生成一份并列对比报告,直观地查看两个数据集之间统计量、分布、缺失值等方面的差异。这非常适用于比较训练集与测试集,或者数据清洗前后的版本。
# 假设我们有原始数据和处理后的数据df_raw = pd.read_csv("titanic.csv")df_clean = df_raw.dropna(subset=['Age', 'Embarked']).copy() # 删除缺失值# 生成对比报告comparison_report = ProfileReport(df_raw, title="Raw vs Cleaned")comparison_report.compare(df_clean, title_clean="Cleaned Data")comparison_report.to_file("comparison_report.html")
7. 敏感数据模式:隐藏原始数据行
当你需要分享报告但希望隐藏具体的原始数据(如包含用户ID、姓名等敏感信息)时,可以设置 sensitive=True。此时报告只会显示统计图表,不会展示任何样本行(首尾行)。
# 假设 df 包含姓名、年龄等字段profile_sensitive = ProfileReport(df, title="Sensitive Mode Report",sensitive=True)profile_sensitive.to_file("sensitive_report.html")
8. 导出 JSON 格式报告
除了 HTML,你还可以将报告导出为 JSON 格式,便于自动化系统读取指标或进一步集成。
# 生成报告后导出 JSONprofile_json = ProfileReport(df, title="JSON Export")json_data = profile_json.to_json()print(json_data[:500]) # 查看前500字符# 或者保存到文件with open("report.json", "w") as f:f.write(profile_json.to_json())
9. 在 Streamlit 应用中集成报告
YData-Profiling 可以与 Streamlit 无缝集成,通过 st.components.v1.html 将报告嵌入到 Web 应用中。
# 这是一个完整的 Streamlit 应用示例import streamlit as stimport pandas as pdfrom ydata_profiling import ProfileReportfrom streamlit.components.v1 import htmlst.title("Data Profiling with YData-Profiling")uploaded_file = st.file_uploader("上传 CSV 文件", type="csv")if uploaded_file is not None:df = pd.read_csv(uploaded_file)st.write("数据预览", df.head())if st.button("生成分析报告"):with st.spinner("正在生成报告..."):profile = ProfileReport(df, title="Uploaded Data Report")# 将报告转为 HTML 字符串report_html = profile.to_html()# 嵌入到 Streamlit 中html(report_html, height=800, scrolling=True)
10. 添加数据集元数据:提升报告可读性
你可以通过 dataset 参数为报告添加描述、作者、URL 等信息,让报告更加专业。
profile = ProfileReport(df,title="Titanic Report",dataset={"description": "泰坦尼克号乘客生存数据集,包含年龄、性别、票价等特征。","copyright_holder": "Data Science Team","url": "https://www.kaggle.com/c/titanic","date": "2025-03-20"})profile.to_file("metadata_report.html")
11. 处理大型数据集的进一步技巧
当数据量非常大时,除了 minimal=True,还可以先对数据进行采样,然后生成报告,或者利用分块处理。
# 方法1:采样df_sample = df.sample(n=10000, random_state=42)profile_sample = ProfileReport(df_sample, minimal=True)profile_sample.to_file("sampled_report.html")# 方法2:分块读取并增量分析(较复杂,适合超大数据)# 官方没有直接支持增量,但可以手动分块,不过一般采样足够
12. 自定义样式/主题
虽然 YData-Profiling 默认使用明亮的主题,但你可以通过修改 CSS 来改变报告的外观。这需要将报告生成为 HTML 字符串后,再注入自定义样式。
# 生成 HTML 字符串html_str = profile.to_html()# 插入自定义 CSS(例如更改主色调)custom_css = """<style>:root {--primary-color: #2c3e50;--secondary-color: #18bc9c;}</style>"""html_str = html_str.replace("</head>", custom_css + "</head>")# 保存修改后的 HTMLwith open("custom_style_report.html", "w") as f:f.write(html_str)
13. 保存报告为 PDF(间接方式)
YData-Profiling 本身不直接支持 PDF 输出,但你可以先生成 HTML,再用浏览器打印功能另存为 PDF,或者使用工具如 pdfkit 将 HTML 转为 PDF。示例:
import pdfkit# 假设你已经有了 report.htmlpdfkit.from_file("report.html", "report.pdf")
注意:pdfkit 需要安装 wkhtmltopdf,请根据你的操作系统安装。
14. 结合 Great Expectations 进行数据验证
你可以将 YData-Profiling 的报告作为数据质量监控的一部分,配合 Great Expectations 使用。例如,先快速生成报告,再根据报告中的警告来编写期望(Expectations)。
# 伪代码示例profile = ProfileReport(df)warnings = profile.get_warnings() # 获取警告列表for warning in warnings:# 根据警告内容编写 Great Expectations 期望print(warning)
15. 命令行快速生成报告
YData-Profiling 也提供了命令行接口,无需编写 Python 代码即可快速生成报告:
# 在终端执行ydata_profiling titanic.csv titanic_report.html