ydata-profiling👉一行代码探索DataFrame奥秘

如果你经常使用pandas分析数据,那么,今天介绍这个工具ydata-profiling,绝对能让你在分析之前,以最快的方式全面了解数据整体的情况。

ydata-profiling的目标是用一行代码就完成数据情况的探索,并输出对数据集的简单且经过消化的分析。

1. 安装方式

通过pip安装:

bash 复制代码
pip install ydata-profiling

或者通过conda安装:

bash 复制代码
conda install -c conda-forge ydata-profiling

2. 分析DataFrame

创建一个随机的数据集(包含重复值和缺失值)。

python 复制代码
import pandas as pd

df1 = pd._testing.makeDataFrame()
df2 = pd._testing.makeMissingDataframe()

# 包含重复数据df1,缺失数据 df2
df = pd.concat([df1, df1, df2], axis=0)
df

然后看看用ydata-profiling探索的结果如何:

python 复制代码
from ydata_profiling import ProfileReport

report = ProfileReport(df, title="随机数据集")
report

默认生成的报告内容,包括:

  1. Overview:数据集概要信息,比如数据量,重复的数据比率,缺失的数据比率等等
  2. Variables:数据集每个列的情况分析
  3. Interactions:不同列之间的数据分布关系
  4. Correlations:不同列之间统计学上的相关性
  5. Missing values:各个列的缺失值统计情况
  6. Sample:数据集中前10行和后10行的数据
  7. Duplicate rows:重复数据的统计

这些内容如果用pandas自带的函数去分析的话,不仅要写很多代码,而且也做不出效果这么好的图文报告。
ydata-profiling为我们在处理数据之前,节约了大量的时间 。

3. 其他常用功能

除了预分析数据集,ydata-profiling还有一些其他的扩展功能,下面列取了其中一些我最近用到的。

(全部的功能可以参考官方文档)

3.1. 数据集比较

比如就用上面随机生成的数据集 df1 和 df2。

python 复制代码
report1 = ProfileReport(df1, title="df1")
report2 = ProfileReport(df2, title="df2")

compare_report = report1.compare(report2)
compare_report

比较数据集之后,生成的分析报告内容和上一节的类似,只是每个部分包含了2个数据集的情况。

3.2. 敏感数据保护

有时候,数据集中包含一些敏感数据,比如手机号,身份证号之类的,我们不希望报告中显示这些数据。

这时,设置sensitive=True,生成的报告中就不会显示数据集中的示例数据了。

python 复制代码
df_time =  pd._testing.makeTimeDataFrame()
df_time

report = ProfileReport(df_time, sensitive=True, title="不显示敏感数据")
report

3.3. 报告样式调整

ydata-profiling默认的报告其实也挺美观的,它还提供了一些参数设置报告的样式(目前样式相关的参数不是很多):

参数 类型 默认值 描述
html.minify_html bool True 如果是True ,则使用 htmlmin 包缩小输出 HTML。
html.use_local_assets bool True 如果 True ,则所有资源(样式表、脚本、图像)都存储在本地。 如果 False ,则 CDN 用于某些样式表和脚本。
html.inline boolean True 如果 True ,则所有资产都包含在报告中。 如果为 False ,则会创建 Web 导出,其中所有资源都存储在"[REPORT_NAME]_assets/"目录中。
html.navbar_show boolean True 报表中是否包含导航栏
html.style.theme string None 选择主题。 可用选项:flatly (dark) 和 united (orange)
html.style.logo string nan Base64 编码的徽标,显示在导航栏中
html.style.primary_color string #337ab7 报告中使用的主要颜色
html.style.full_width boolean False 默认情况下,报告的宽度是固定的。 如果设置为 True ,则使用屏幕的整个宽度。

设置属性的方式如下:

python 复制代码
report = ProfileReport(df_time, sensitive=True, title="不显示敏感数据")

# 比如设置不显示导航栏
report.config.html.navbar_show = False

4. 报告导出

最后是报告导出,一般导出html格式:

python 复制代码
with open("output.html", "w") as f:
    f.write(report.to_html())

或者更直接的方式如下:

python 复制代码
report.to_file("output.html")
相关推荐
SelectDB技术团队20 分钟前
SelectDB 架构演进与 ClickHouse 迁移指南
大数据·数据库·clickhouse·云原生·架构·数据分析·apache doris
Yolanda_202223 分钟前
PyTorch 线性层nn.Linear底层数学原理
人工智能·pytorch·python
软件技术NINI23 分钟前
基于JavaWeb 汽车租聘
java·vue.js·spring boot·后端·python·毕业设计·毕设
quantdash_cc30 分钟前
如何从多只股票的历史行情构建收益率矩阵?Python 与 QuantDash 实战教程
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
专注仿真43 分钟前
多智能体强化学习实战:从 Dec-POMDP 到 QMIX/MADDPG/MAPPO 的可运行 PyTorch 实现
人工智能·pytorch·python
不是万能的豆学长4 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
gCode Teacher 格码致知6 小时前
Pandas教学-18:number与numeric的区别
python·pandas
梦想不只是梦与想8 小时前
Python 中subprocess 模块:执行外部命令
python·subprocess·调用外部命令
懂点Tech9 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
residual_fan13 小时前
船用柴油机动态机动工况故障诊断:控制感知仿射网络(CAAN)的技术实现
人工智能·算法·数据挖掘·数据分析