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

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

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

     数据集地址: 

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

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

     看一下数据长啥样:

import numpy as np	
import pandas as pd	
adult = pd.read_csv('../adult.data')	
adult.head()

640?wx_fmt=png

     对数据进行统计描述:

adult.describe()

640?wx_fmt=png

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

adult.info()

640?wx_fmt=png

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

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

pip:

pip install pandas-profiling	
pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip

conda:

conda install -c conda-forge pandas-profiling

source:

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

python setup.py install

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

profile = df.profile_report(title="Census Dataset")	
profile.to_file(output_file=Path("./census_report.html"))

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

数据整体概览:

640?wx_fmt=png

变量探索:

640?wx_fmt=png

相关性计算:

640?wx_fmt=png

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

缺失值情况:

640?wx_fmt=png

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

数据样本展示:

640?wx_fmt=png

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

上述示例参考代码:

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配置方法:

640?wx_fmt=png

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

640?wx_fmt=png

参考资料:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值