Rozeeeee commited on
Commit
0e115ff
·
verified ·
1 Parent(s): 9a16016

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -58
app.py CHANGED
@@ -1,81 +1,51 @@
1
  import streamlit as st
2
  import pandas as pd
 
3
  import requests
4
  import plotly.express as px
5
  import io
6
 
7
  # 設置全局主題
8
- THEME = px.colors.qualitative.Bold
9
 
10
- # 下載並載入 CSV 數據
11
- @st.cache_data
12
- def download_and_clean_csv(url):
13
  response = requests.get(url)
14
  response.encoding = 'utf-8'
15
  df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
16
-
17
- # 資料清理
18
- df = df.dropna().drop_duplicates() # 移除缺失值與重複數據
19
- df.columns = df.columns.str.strip() # 去除欄位名稱前後空格
20
- df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x) # 去除字串內的空格
21
-
22
- # 嘗試轉換數值欄位
23
- num_cols = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
24
- for col in num_cols:
25
- if col in df.columns:
26
- df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0)
27
-
28
  return df
29
 
30
- # 美化圖表
31
- def beautify_chart(fig):
32
- fig.update_layout(
33
- font_family="Arial",
34
- font_color="#444",
35
- title_font_family="Arial",
36
- title_font_color="#000",
37
- legend_title_font_color="#000",
38
- plot_bgcolor='rgba(0,0,0,0)',
39
- paper_bgcolor='rgba(0,0,0,0)',
40
- )
41
- fig.update_xaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
42
- fig.update_yaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
43
- return fig
44
 
45
- # 下載、清理並合併數據
 
 
 
 
 
46
  urls = [
47
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
48
  "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
49
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
50
  ]
51
- dfs = [download_and_clean_csv(url) for url in urls]
52
- combined_df = pd.concat(dfs, ignore_index=True)
53
-
54
- # 進一步清理 combined_df
55
- combined_df = combined_df.dropna().drop_duplicates()
56
- combined_df.columns = combined_df.columns.str.strip()
57
- combined_df = combined_df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
58
- for col in ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]:
59
- if col in combined_df.columns:
60
- combined_df[col] = pd.to_numeric(combined_df[col], errors='coerce').fillna(0)
61
 
62
- # 合併數據
63
- df_final = combined_df.groupby("公司名稱", as_index=False).sum()
64
 
65
- # 設定 Streamlit 介面
66
- st.title("台灣企業ESG數據分析與揭露")
67
 
68
  # 顯示清理後的數據
69
- st.subheader("清理後的數據預覽")
70
- st.dataframe(df_final)
71
-
72
- # 可視化數據
73
- emission_columns = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
74
- selected_columns = [col for col in emission_columns if col in df_final.columns]
75
-
76
- if selected_columns:
77
- st.subheader("排放量折線圖")
78
- fig_line = px.line(df_final, x="公司名稱", y=selected_columns, title="企業排放量趨勢", color_discrete_sequence=THEME)
79
- st.plotly_chart(beautify_chart(fig_line))
80
- else:
81
- st.write("未找到有效的排放數據")
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import numpy as np
4
  import requests
5
  import plotly.express as px
6
  import io
7
 
8
  # 設置全局主題
9
+ theme = px.colors.qualitative.Bold
10
 
11
+ # 下載 CSV 數據
12
+ def download_and_load_csv(url):
 
13
  response = requests.get(url)
14
  response.encoding = 'utf-8'
15
  df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
16
  return df
17
 
18
+ # 清理數據並合併
19
+ def clean_and_merge_data(urls):
20
+ dfs = [download_and_load_csv(url) for url in urls]
21
+
22
+ # 合併所有數據
23
+ combined_df = pd.concat(dfs, ignore_index=True)
24
+
25
+ # 指定要清理的排放量欄位
26
+ emission_columns = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
27
+
28
+ # 只清理 emission_columns 內的 0 值,不影響其他欄位
29
+ combined_df[emission_columns] = combined_df[emission_columns].replace(0, np.nan).dropna(subset=emission_columns)
 
 
30
 
31
+ # 依據 "公司名稱" 進行合併,將相同公司名稱的數據進行加總
32
+ merged_df = combined_df.groupby("公司名稱", as_index=False).sum()
33
+
34
+ return merged_df
35
+
36
+ # CSV 來源
37
  urls = [
38
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_1.csv",
39
  "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv",
40
  "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
41
  ]
 
 
 
 
 
 
 
 
 
 
42
 
43
+ # 清理並合併數據
44
+ cleaned_df = clean_and_merge_data(urls)
45
 
46
+ # 設置 Streamlit 標題
47
+ st.title("台灣企業 ESG 數據分析與揭露")
48
 
49
  # 顯示清理後的數據
50
+ st.subheader("清理 & 合併後的數據預覽")
51
+ st.dataframe(cleaned_df)