前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pandas_profiling:一行代码生成你的数据分析报告

pandas_profiling:一行代码生成你的数据分析报告

作者头像
石晓文
发布2019-10-11 23:44:53
7420
发布2019-10-11 23:44:53
举报
文章被收录于专栏:小小挖掘机小小挖掘机

笔者最近发现一款将pandas数据框快速转化为描述性数据分析报告的package——pandas_profiling。一行代码即可生成内容丰富的EDA内容,两行代码即可将报告以.html格式保存。笔者当初也是从数据分析做起的,所以深知这个工具对于数据分析的朋友而言极为方便,在此特地分享给大家。

我们以uci机器学习库中的人口调查数据集adult.data为例进行说明。

数据集地址:

https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data

常规情况下我们拿到数据做EDA的时候这几种函数是必用的:

看一下数据长啥样:

代码语言:javascript
复制
import numpy as np
import pandas as pd
adult = pd.read_csv('../adult.data')
adult.head()

对数据进行统计描述:

代码语言:javascript
复制
adult.describe()

查看变量信息和缺失情况:

代码语言:javascript
复制
adult.info()

这是最简单最快速了解一个数据集的方法。当然,更深层次的EDA一定是要借助统计图形来展示的。基于scipy、matplotlib和seaborn等工具的展示这里权且略过。

现在我们有了pandas_profiling。上述过程以及各种统计相关性计算、统计绘图全部由pandas_profiling打包搞定了。pandas_profiling安装,包括pip、conda和源码三种安装方式。

pip:

代码语言:javascript
复制
pip install pandas-profiling
pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip

conda:

代码语言:javascript
复制
conda install -c conda-forge pandas-profiling

source:

先下载源码文件,然后解压到setup.py所在的文件目录下:

代码语言:javascript
复制
python setup.py install

再来看pandas_profiling基本用法,用pandas将数据读入之后,对数据框直接调用profile_report方法生成EDA分析报告,然后使用to_file方法另存为.html文件。

代码语言:javascript
复制
profile = df.profile_report(title="Census Dataset")
profile.to_file(output_file=Path("./census_report.html"))

看看报告效果如何。pandas-profiling EDA报告包括数据整体概览、变量探索、相关性计算、缺失值情况和抽样展示等5个方面。

数据整体概览:

变量探索:

相关性计算:

这里为大家提供5种相关性系数。

缺失值情况:

pandas-profiling为我们提供了四种缺失值展现形式。

数据样本展示:

就是pandas里面的df.head()和df.tail()两个函数。

上述示例参考代码:

代码语言:javascript
复制
from pathlib import Path
import pandas as pd
import numpy as np
import requests
import pandas_profiling
if __name__ == "__main__":
    file_name = Path("census_train.csv")
    if not file_name.exists():
        data = requests.get(
            "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
        )
        file_name.write_bytes(data.content)
    # Names based on https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.names

    df = pd.read_csv(
        file_name,
        header=None,
        index_col=False,
        names=[
            "age",
            "workclass",
            "fnlwgt",
            "education",
            "education-num",
            "marital-status",
            "occupation",
            "relationship",
            "race",
            "sex",
            "capital-gain",
            "capital-loss",
            "hours-per-week",
            "native-country",
        ],
    )
    # Prepare missing values
    df = df.replace("\\?", np.nan, regex=True)
    profile = df.profile_report(title="Census Dataset")
    profile.to_file(output_file=Path("./census_report.html"))

除此之外,pandas_profiling还提供了pycharm配置方法:

配置完成后在pycharm左边项目栏目直接右键external_tool下的pandas_profiling即可直接生成EDA报告。更多内容大家可以到该项目GitHub地址查看:

参考资料:

https://github.com/pandas-profiling/pandas-profiling

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-05,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 小小挖掘机 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com