利用Python自制網(wǎng)頁(yè)并實(shí)現(xiàn)一鍵自動(dòng)生成探索性數(shù)據(jù)分析報(bào)告
前言
今天小編帶領(lǐng)大家用Python自制一個(gè)自動(dòng)生成探索性數(shù)據(jù)分析報(bào)告這樣的一個(gè)工具,大家只需要在瀏覽器中輸入url便可以輕松的訪問(wèn),如下所示:
第一步
首先我們導(dǎo)入所要用到的模塊,設(shè)置網(wǎng)頁(yè)的標(biāo)題、工具欄以及l(fā)ogo的導(dǎo)入,代碼如下:
from st_aggrid import AgGrid import streamlit as st import pandas as pd import pandas_profiling from streamlit_pandas_profiling import st_profile_report from pandas_profiling import ProfileReport from ?PIL import Image st.set_page_config(layout='wide') #Choose wide mode as the default setting #Add a logo (optional) in the sidebar logo = Image.open(r'wechat_logo.jpg') st.sidebar.image(logo, ?width=120) #Add the expander to provide some information about the app with st.sidebar.expander("關(guān)于這個(gè)項(xiàng)目"): ? ? ?st.write(""" ? ? ? ? 該項(xiàng)目是將streamlit和pandas_profiling相結(jié)合,在您上傳數(shù)據(jù)集之后自動(dòng)生成相關(guān)的數(shù)據(jù)分析報(bào)告,當(dāng)然該項(xiàng)目提供了兩種模式 全量分析還是部分少量分析,這里推薦用部分少量分析,因?yàn)橛?jì)算量更少,所需要的時(shí)間更短,效率更高 ? ? ?""") #Add an app title. Use css to style the title st.markdown(""" <style> .font { ? ? font-size:30px ; font-family: 'Cooper Black'; color: #FF9633;} ? ? </style> """, unsafe_allow_html=True) st.markdown('<p class="font">請(qǐng)上傳您的數(shù)據(jù)集,該應(yīng)用會(huì)自動(dòng)生成相關(guān)的數(shù)據(jù)分析報(bào)告</p>', unsafe_allow_html=True)
output:
上傳文件以及變量的篩選
緊接的是我們需要上傳csv文件,代碼如下:
uploaded_file = st.file_uploader("請(qǐng)上傳您的csv文件: ", type=['csv'])
我們可以選擇針對(duì)數(shù)據(jù)集當(dāng)中所有的特征進(jìn)行一個(gè)統(tǒng)計(jì)分析,或者只是針對(duì)部分的變量來(lái)一個(gè)數(shù)據(jù)分析,
代碼如下:
if uploaded_file is not None: ? ? ?df = pd.read_csv(uploaded_file) ? ? ?option1 = st.sidebar.radio( ? ? ? ? ? '您希望您的數(shù)據(jù)分析報(bào)告中包含哪些變量呢', ? ? ? ? ? ('所有變量', '部分變量')) ? ? ? ?if option1 == '所有變量': ? ? ? ? ? df = df ? ? ?elif option1 == '部分變量': ? ? ? ? ? var_list = list(df.columns)
要是用戶勾選的是部分變量,只是針對(duì)部分變量來(lái)進(jìn)行一個(gè)分析的話,就會(huì)彈出來(lái)一個(gè)多選框來(lái)供用戶選擇,
代碼如下:
var_list = list(df.columns) option3 = st.sidebar.multiselect( ? ? ?'篩選出您希望在數(shù)據(jù)分析報(bào)告中包含的變量', ? ? ?var_list) df = df[option3]
用戶可以挑選到底是“簡(jiǎn)單分析”或者是“完整分析”,要是勾選的是“完整分析”的話,會(huì)跳出相應(yīng)的提示,提示“完整分析”由于涉及到更加復(fù)雜的計(jì)算操作,耗時(shí)更加地長(zhǎng),要是遇到大型的數(shù)據(jù)集,還會(huì)有計(jì)算失敗的情況出現(xiàn)
option2 = st.sidebar.selectbox( ? ? ? '篩選模式,完整分析還是簡(jiǎn)單分析', ? ? ? ('簡(jiǎn)單分析', '完整分析')) ?if option2 == '完整分析': ? ? ? mode = 'complete' ? ? ? st.sidebar.warning( ? ? ? ? ? ?'完整分析由于涉及到更加復(fù)雜的計(jì)算操作,耗時(shí)更加地長(zhǎng),要是遇到大型的數(shù)據(jù)集,還會(huì)有計(jì)算失敗的情況出現(xiàn),這里推薦使用簡(jiǎn)單分析') ?elif option2 == '簡(jiǎn)單分析': ? ? ? mode = 'minimal' ? ? ? grid_response = AgGrid( ? ? ? ? ? ?df, ? ? ? ? ? ?editable=True, ? ? ? ? ? ?height=300, ? ? ? ? ? ?width='100%', ? ? ? ) ? ? ? updated = grid_response['data'] ? ? ? df1 = pd.DataFrame(updated)
當(dāng)用戶點(diǎn)擊“生成報(bào)告”的時(shí)候就會(huì)自動(dòng)生成一份完整的數(shù)據(jù)分析報(bào)告了,代碼如下:
if st.button('生成報(bào)告'): ? ? ? ? if mode=='complete': ? ? ? ? ? ? profile=ProfileReport(df, ? ? ? ? ? ? ? ? title="User uploaded table", ? ? ? ? ? ? ? ? progress_bar=True, ? ? ? ? ? ? ? ? dataset={ ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? st_profile_report(profile) ? ? ? ? elif mode=='minimal': ? ? ? ? ? ? profile=ProfileReport(df1, ? ? ? ? ? ? ? ? minimal=True, ? ? ? ? ? ? ? ? title="User uploaded table", ? ? ? ? ? ? ? ? progress_bar=True, ? ? ? ? ? ? ? ? dataset={ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? st_profile_report(profile)
最后出來(lái)的結(jié)果如下:
到此這篇關(guān)于利用Python自制了網(wǎng)頁(yè)并實(shí)現(xiàn)一鍵自動(dòng)生成探索性數(shù)據(jù)分析報(bào)告的文章就介紹到這了,更多相關(guān) Python自制網(wǎng)頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Socket編程實(shí)現(xiàn)猜數(shù)字游戲交互體驗(yàn)
當(dāng)利用Python的Socket編程創(chuàng)建一個(gè)猜數(shù)字游戲時(shí),需要分別實(shí)現(xiàn)服務(wù)器端和客戶端的邏輯,本文將詳細(xì)描述這兩個(gè)部分的功能和代碼片段2024-01-01網(wǎng)站滲透常用Python小腳本查詢同ip網(wǎng)站
這篇文章主要介紹了網(wǎng)站滲透常用Python小腳本查詢同ip網(wǎng)站,需要的朋友可以參考下2017-05-05利用selenium 3.7和python3添加cookie模擬登陸的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于利用selenium 3.7和python3添加cookie模擬登陸的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11在Lighttpd服務(wù)器中運(yùn)行Django應(yīng)用的方法
這篇文章主要介紹了在Lighttpd服務(wù)器中運(yùn)行Django應(yīng)用的方法,本文所采用的是最流行的FastCGI模塊,包括同時(shí)運(yùn)行多個(gè)Django應(yīng)用的方法,需要的朋友可以參考下2015-07-07Tensorflow中批量讀取數(shù)據(jù)的案列分析及TFRecord文件的打包與讀取
這篇文章主要介紹了Tensorflow中批量讀取數(shù)據(jù)的案列分析及TFRecord文件的打包與讀取,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝
這篇文章主要為大家介紹了Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝,感興趣的小伙伴們可以參考一下2016-04-04基于Python+Turtle實(shí)現(xiàn)繪制簡(jiǎn)易的大風(fēng)車
大風(fēng)車,吱呀吱呦呦地轉(zhuǎn),這里的風(fēng)景呀真好看!天好看,地好看……一首熟悉的歌曲,是否已經(jīng)把你拉回了童年?本文將用Turtle庫(kù)繪制簡(jiǎn)易的大風(fēng)車,需要的可以參考一下2022-06-06pycharm中使用anaconda部署python環(huán)境的方法步驟
這篇文章主要介紹了pycharm中使用anaconda部署python環(huán)境的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12