利用Python自制網(wǎng)頁并實現(xiàn)一鍵自動生成探索性數(shù)據(jù)分析報告
前言
今天小編帶領(lǐng)大家用Python自制一個自動生成探索性數(shù)據(jù)分析報告這樣的一個工具,大家只需要在瀏覽器中輸入url便可以輕松的訪問,如下所示:

第一步
首先我們導(dǎo)入所要用到的模塊,設(shè)置網(wǎng)頁的標(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)于這個項目"):
? ? ?st.write("""
? ? ? ? 該項目是將streamlit和pandas_profiling相結(jié)合,在您上傳數(shù)據(jù)集之后自動生成相關(guān)的數(shù)據(jù)分析報告,當(dāng)然該項目提供了兩種模式 全量分析還是部分少量分析,這里推薦用部分少量分析,因為計算量更少,所需要的時間更短,效率更高
? ? ?""")
#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">請上傳您的數(shù)據(jù)集,該應(yīng)用會自動生成相關(guān)的數(shù)據(jù)分析報告</p>', unsafe_allow_html=True)output:

上傳文件以及變量的篩選
緊接的是我們需要上傳csv文件,代碼如下:
uploaded_file = st.file_uploader("請上傳您的csv文件: ", type=['csv'])我們可以選擇針對數(shù)據(jù)集當(dāng)中所有的特征進行一個統(tǒng)計分析,或者只是針對部分的變量來一個數(shù)據(jù)分析,
代碼如下:
if uploaded_file is not None:
? ? ?df = pd.read_csv(uploaded_file)
? ? ?option1 = st.sidebar.radio(
? ? ? ? ? '您希望您的數(shù)據(jù)分析報告中包含哪些變量呢',
? ? ? ? ? ('所有變量', '部分變量'))
?
? ? ?if option1 == '所有變量':
? ? ? ? ? df = df
? ? ?elif option1 == '部分變量':
? ? ? ? ? var_list = list(df.columns)要是用戶勾選的是部分變量,只是針對部分變量來進行一個分析的話,就會彈出來一個多選框來供用戶選擇,
代碼如下:
var_list = list(df.columns) option3 = st.sidebar.multiselect( ? ? ?'篩選出您希望在數(shù)據(jù)分析報告中包含的變量', ? ? ?var_list) df = df[option3]
用戶可以挑選到底是“簡單分析”或者是“完整分析”,要是勾選的是“完整分析”的話,會跳出相應(yīng)的提示,提示“完整分析”由于涉及到更加復(fù)雜的計算操作,耗時更加地長,要是遇到大型的數(shù)據(jù)集,還會有計算失敗的情況出現(xiàn)
option2 = st.sidebar.selectbox(
? ? ? '篩選模式,完整分析還是簡單分析',
? ? ? ('簡單分析', '完整分析'))
?if option2 == '完整分析':
? ? ? mode = 'complete'
? ? ? st.sidebar.warning(
? ? ? ? ? ?'完整分析由于涉及到更加復(fù)雜的計算操作,耗時更加地長,要是遇到大型的數(shù)據(jù)集,還會有計算失敗的情況出現(xiàn),這里推薦使用簡單分析')
?elif option2 == '簡單分析':
? ? ? mode = 'minimal'
? ? ? grid_response = AgGrid(
? ? ? ? ? ?df,
? ? ? ? ? ?editable=True,
? ? ? ? ? ?height=300,
? ? ? ? ? ?width='100%',
? ? ? )
? ? ? updated = grid_response['data']
? ? ? df1 = pd.DataFrame(updated)當(dāng)用戶點擊“生成報告”的時候就會自動生成一份完整的數(shù)據(jù)分析報告了,代碼如下:
if st.button('生成報告'):
? ? ? ? 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)最后出來的結(jié)果如下:

到此這篇關(guān)于利用Python自制了網(wǎng)頁并實現(xiàn)一鍵自動生成探索性數(shù)據(jù)分析報告的文章就介紹到這了,更多相關(guān) Python自制網(wǎng)頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用python打開攝像頭并把圖像傳回qq郵箱(Pyinstaller打包)
這篇文章主要介紹了用python打開攝像頭并把圖像傳回qq郵箱,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
python?Ajenti控制面板輕松地管理所有服務(wù)器網(wǎng)站
Ajenti是一個值得擁有的管理面板,免費開源的管理面板工具,可以幫助你集中管理多個服務(wù)器和網(wǎng)站,Ajenti?支持?Linux、BSD、Mac?OS?X和Windows?等多個操作系統(tǒng),并且可以通過一個直觀的?Web?界面來完成各種系統(tǒng)管理任務(wù)2024-01-01
python中pathlib 面向?qū)ο蟮奈募到y(tǒng)路徑
文章介紹了Python 3.4+內(nèi)置的標(biāo)準(zhǔn)庫pathlib,該庫提供面向?qū)ο蟮奈募到y(tǒng)路徑處理,通過pathlib,可以更方便地進行路徑操作,感興趣的朋友一起看看吧2024-12-12

