Rooobert commited on
Commit
dd5d47e
·
verified ·
1 Parent(s): 14a3c3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -7,7 +7,6 @@ from google.oauth2.service_account import Credentials
7
  import gspread
8
  import plotly.express as px
9
  import plotly.graph_objects as go
10
- from plotly.subplots import make_subplots
11
 
12
  # Google Sheets credentials
13
  SCOPE = ['https://www.googleapis.com/auth/spreadsheets']
@@ -89,12 +88,16 @@ def clean_rating(x):
89
  return float(str(x).replace('分數', '').replace('分', ''))
90
 
91
  def create_price_rating_scatter(df):
 
 
 
 
92
  fig = px.scatter(
93
  df,
94
  x='價格',
95
  y='評分',
96
  text='飯店名稱',
97
- size='價格',
98
  color='評分',
99
  title='台南飯店價格與評分關係圖',
100
  labels={'價格': '房價 (TWD)', '評分': '評分 (0-10)'}
@@ -142,8 +145,17 @@ elif mode == "資料視覺化":
142
  st.header("分析與視覺化")
143
  try:
144
  df = pd.read_csv('booking_hotels_tainan.csv', encoding='utf-8-sig')
145
- df['價格'] = pd.to_numeric(df['價格'], errors='coerce')
146
- df['評分'] = df['評分'].apply(clean_rating)
 
 
 
 
 
 
 
 
 
147
  st.plotly_chart(create_price_rating_scatter(df))
148
  st.plotly_chart(create_price_distribution(df))
149
  except Exception as e:
 
7
  import gspread
8
  import plotly.express as px
9
  import plotly.graph_objects as go
 
10
 
11
  # Google Sheets credentials
12
  SCOPE = ['https://www.googleapis.com/auth/spreadsheets']
 
88
  return float(str(x).replace('分數', '').replace('分', ''))
89
 
90
  def create_price_rating_scatter(df):
91
+ if df.empty:
92
+ st.warning("沒有可視化的有效數據。請檢查輸入資料。")
93
+ return None # 無數據時返回空
94
+
95
  fig = px.scatter(
96
  df,
97
  x='價格',
98
  y='評分',
99
  text='飯店名稱',
100
+ size='價格', # 點的大小根據價格變化
101
  color='評分',
102
  title='台南飯店價格與評分關係圖',
103
  labels={'價格': '房價 (TWD)', '評分': '評分 (0-10)'}
 
145
  st.header("分析與視覺化")
146
  try:
147
  df = pd.read_csv('booking_hotels_tainan.csv', encoding='utf-8-sig')
148
+
149
+ # 清理數據
150
+ df['價格'] = pd.to_numeric(df['價格'], errors='coerce') # 無效值轉為 NaN
151
+ df['評分'] = df['評分'].apply(clean_rating) # 清理評分數據
152
+ df = df.dropna(subset=['價格']) # 移除價格為 NaN 的行
153
+
154
+ # 顯示數據摘要
155
+ st.write(f"有效數據行數:{len(df)}")
156
+ st.write(f"價格缺失值數量:{df['價格'].isna().sum()}")
157
+
158
+ # 繪製圖表
159
  st.plotly_chart(create_price_rating_scatter(df))
160
  st.plotly_chart(create_price_distribution(df))
161
  except Exception as e: