使用Python及R语言绘制简易数据分析报告

发布于:2024-05-09 ⋅ 阅读:(23) ⋅ 点赞:(0)
Pytohn实现

  在python中有很多包可以实现绘制数据分析报告的功能,推荐两个较为方便的包:pandas-profiling 和 sweetviz 。

使用 pandas-profiling 包(功能全面)

    这个包的个别依赖包与机器学习的 sklearn 包的依赖包存在版本冲突,如已安装sklearn包的话可以在Anaconda中创建虚拟环境使用。

在Anaconda中创建python虚拟环境(创建了一个3.6版本,名为GGBoy的python虚拟环境):

conda create -n GGBoy python=3.6

Proceed([y]/n)? 中选择 y 

创建虚拟环境成功后激活虚拟环境:

conda activate GGBoy

 环境激活成功:

(GGBoy) C:\Users\114514>

在虚拟环境中安装pandas包和pandas-profiling包:

conda install pandas
pip install pandas-profiling

 绘制titandick数据集的数据报告,数据集下载地址:taitanic | Kaggle

在VSCode中建立一个拓展名.ipynb的jupyter notebook文件,内核选择刚才新建的虚拟环境:

 使用 pandas-profiling 包:

from pandas_profiling import ProfileReport
import pandas as pd

# 使用pandas读取titandick的csv数据集
data = pd.read_csv('C:\\Users\\114514\\Desktop\\titandick.csv')
df = pd.DataFrame(data)

# 绘制数据分析报告
report = ProfileReport(df)  
report.to_file(output_file='report.html')

 生成报告:

使用 sweetviz 包(最方便操作):

import sweetviz as sv
import pandas as pd

# 使用pandas读取titandick的csv数据集
data = pd.read_csv('C:\\Users\\114514\\Desktop\\titandick.csv')
df = pd.DataFrame(data)

# 使用sweetviz包绘制数据分析报告
report = sv.analyze(df) 
report.show_html('report.html')

生成报告:

R语言实现

 需要先下载R包 summarytools :

install.packages("summarytools")
library(summarytools)

df <- read.csv("C:/Users/114514/Desktop/titandick.csv")
report <- dfSummary(df)  

html_report <- print(report, method = "render", include.row.numbers = FALSE, style = "grid")  

html_output <- paste(html_report, collapse = "\n")  

# 将合并后的HTML字符串写入文件  
cat(html_output, file = "report_GGBoy.html")

 生成的数据分析报告: