Rooobert commited on
Commit
bdf7d63
·
verified ·
1 Parent(s): e985e74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
app.py CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
  from dataclasses import dataclass, field
5
- import numpy as np
6
  from typing import Dict, Tuple, Any
7
 
8
  # 📥 讀取 Google 試算表函數
@@ -43,6 +42,22 @@ class SurveyAnalyzer:
43
  '6.對於示範場域的服務感到滿意'
44
  ]
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def plot_satisfaction_correlation(self, df: pd.DataFrame):
47
  """🔥 滿意度相關性熱力圖"""
48
  correlation_matrix = df[self.satisfaction_columns].corr()
@@ -60,21 +75,13 @@ class SurveyAnalyzer:
60
 
61
  st.plotly_chart(fig, use_container_width=True)
62
 
63
- def generate_report(self, df: pd.DataFrame) -> Dict[str, Any]:
64
- """📝 生成問卷調查報告"""
65
- return {
66
- '基本統計': {
67
- '總受訪人數': len(df),
68
- '性別分布': df['1. 性別'].value_counts().to_dict(),
69
- '教育程度分布': df['3.教育程度'].value_counts().to_dict(),
70
- '平均年齡': f"{pd.to_numeric(df['2.出生年(民國__年)'], errors='coerce').mean():.1f}歲"
71
- },
72
- '滿意度統計': {
73
- '整體平均滿意度': f"{df['6.對於示範場域的服務感到滿意'].mean():.2f}",
74
- '最高分項目': df[self.satisfaction_columns].mean().idxmax(),
75
- '最低分項目': df[self.satisfaction_columns].mean().idxmin()
76
- }
77
- }
78
 
79
  # 🎨 Streamlit UI
80
  def main():
@@ -90,16 +97,25 @@ def main():
90
  analyzer = SurveyAnalyzer()
91
 
92
  # 📌 基本統計數據
93
- st.header("📋 問卷統計報告")
94
- report = analyzer.generate_report(df)
95
- for category, stats in report.items():
96
- with st.expander(f"🔍 {category}"):
97
- for key, value in stats.items():
98
- st.write(f"**{key}**: {value}")
99
-
100
- # 📊 滿意度熱力圖
101
- st.header("🔥 滿意度相關性熱力圖")
102
- analyzer.plot_satisfaction_correlation(df)
 
 
 
 
 
 
 
 
 
103
 
104
  if __name__ == "__main__":
105
  main()
 
2
  import pandas as pd
3
  import plotly.express as px
4
  from dataclasses import dataclass, field
 
5
  from typing import Dict, Tuple, Any
6
 
7
  # 📥 讀取 Google 試算表函數
 
42
  '6.對於示範場域的服務感到滿意'
43
  ]
44
 
45
+ def generate_report(self, df: pd.DataFrame) -> Dict[str, Any]:
46
+ """📝 生成問卷調查報告"""
47
+ return {
48
+ '基本統計': {
49
+ '總受訪人數': len(df),
50
+ '性別分布': df['1. 性別'].value_counts().to_dict(),
51
+ '教育程度分布': df['3.教育程度'].value_counts().to_dict(),
52
+ '平均年齡': f"{pd.to_numeric(df['2.出生年(民國__年)'], errors='coerce').mean():.1f}歲"
53
+ },
54
+ '滿意度統計': {
55
+ '整體平均滿意度': f"{df['6.對於示範場域的服務感到滿意'].mean():.2f}",
56
+ '最高分項目': df[self.satisfaction_columns].mean().idxmax(),
57
+ '最低分項目': df[self.satisfaction_columns].mean().idxmin()
58
+ }
59
+ }
60
+
61
  def plot_satisfaction_correlation(self, df: pd.DataFrame):
62
  """🔥 滿意度相關性熱力圖"""
63
  correlation_matrix = df[self.satisfaction_columns].corr()
 
75
 
76
  st.plotly_chart(fig, use_container_width=True)
77
 
78
+ def plot_gender_distribution(self, df: pd.DataFrame):
79
+ """🟠 性別分佈圓餅圖"""
80
+ gender_counts = df['1. 性別'].value_counts().reset_index()
81
+ gender_counts.columns = ['性別', '人數']
82
+ fig = px.pie(gender_counts, names='性別', values='人數', title='🟠 受訪者性別分布',
83
+ color_discrete_sequence=px.colors.sequential.Sunset)
84
+ st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
85
 
86
  # 🎨 Streamlit UI
87
  def main():
 
97
  analyzer = SurveyAnalyzer()
98
 
99
  # 📌 基本統計數據
100
+ st.sidebar.header("📌 選擇數據分析")
101
+ selected_analysis = st.sidebar.radio("選擇要查看的分析",
102
+ ["📋 問卷統計報告", "🔥 滿意度相關性熱力圖", "🟠 性別分佈"])
103
+
104
+ if selected_analysis == "📋 問卷統計報告":
105
+ st.header("📋 問卷統計報告")
106
+ report = analyzer.generate_report(df)
107
+ for category, stats in report.items():
108
+ with st.expander(f"🔍 {category}"):
109
+ for key, value in stats.items():
110
+ st.write(f"**{key}**: {value}")
111
+
112
+ elif selected_analysis == "🔥 滿意度相關性熱力圖":
113
+ st.header("🔥 滿意度相關性熱力圖")
114
+ analyzer.plot_satisfaction_correlation(df)
115
+
116
+ elif selected_analysis == "🟠 性別分佈":
117
+ st.header("🟠 性別分佈")
118
+ analyzer.plot_gender_distribution(df)
119
 
120
  if __name__ == "__main__":
121
  main()