Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,17 +9,17 @@ from pytrends.request import TrendReq
|
|
9 |
import plotly.graph_objs as go
|
10 |
import plotly.express as px
|
11 |
|
12 |
-
# 新增函數:準備
|
13 |
def prepare_sunburst_data(df):
|
14 |
# 將價格分成不同區間
|
15 |
df['price_range'] = pd.cut(df['price'], bins=[0, 10000, 20000, 30000, 40000, float('inf')],
|
16 |
labels=['0-10k', '10k-20k', '20k-30k', '30k-40k', '40k+'])
|
17 |
|
18 |
-
#
|
19 |
-
sunburst_data = df.groupby(['source', 'price_range'
|
20 |
|
21 |
# 添加一個根節點
|
22 |
-
root = pd.DataFrame({'source': ['Total'], 'price_range': [''], '
|
23 |
sunburst_data = pd.concat([root, sunburst_data])
|
24 |
|
25 |
return sunburst_data
|
@@ -234,9 +234,9 @@ if st.button("開始搜索"):
|
|
234 |
|
235 |
# 準備並繪製 Sunburst 圖
|
236 |
sunburst_data = prepare_sunburst_data(combined_data)
|
237 |
-
fig5 = px.sunburst(sunburst_data, path=['source', 'price_range'
|
238 |
title=f'"{search_keyword}" 價格分佈 Sunburst 圖')
|
239 |
st.plotly_chart(fig5)
|
240 |
|
241 |
# 添加互動性說明
|
242 |
-
st.write("提示:點擊 Sunburst 圖的各個部分可以深入查看細節。雙擊可以返回上一層。")
|
|
|
9 |
import plotly.graph_objs as go
|
10 |
import plotly.express as px
|
11 |
|
12 |
+
# 新增函數:準備sunburst圖的數據
|
13 |
def prepare_sunburst_data(df):
|
14 |
# 將價格分成不同區間
|
15 |
df['price_range'] = pd.cut(df['price'], bins=[0, 10000, 20000, 30000, 40000, float('inf')],
|
16 |
labels=['0-10k', '10k-20k', '20k-30k', '30k-40k', '40k+'])
|
17 |
|
18 |
+
# 計算每個來源和價格區間的商品數量
|
19 |
+
sunburst_data = df.groupby(['source', 'price_range']).size().reset_index(name='count')
|
20 |
|
21 |
# 添加一個根節點
|
22 |
+
root = pd.DataFrame({'source': ['Total'], 'price_range': [''], 'count': [sunburst_data['count'].sum()]})
|
23 |
sunburst_data = pd.concat([root, sunburst_data])
|
24 |
|
25 |
return sunburst_data
|
|
|
234 |
|
235 |
# 準備並繪製 Sunburst 圖
|
236 |
sunburst_data = prepare_sunburst_data(combined_data)
|
237 |
+
fig5 = px.sunburst(sunburst_data, path=['source', 'price_range'], values='count',
|
238 |
title=f'"{search_keyword}" 價格分佈 Sunburst 圖')
|
239 |
st.plotly_chart(fig5)
|
240 |
|
241 |
# 添加互動性說明
|
242 |
+
st.write("提示:點擊 Sunburst 圖的各個部分可以深入查看細節。雙擊可以返回上一層。")
|