Spaces:
Sleeping
Sleeping
danielrosehill
commited on
Commit
·
995d03d
1
Parent(s):
2aacdd5
updated
Browse files
app.py
CHANGED
@@ -1,65 +1,95 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import
|
|
|
4 |
|
5 |
-
#
|
6 |
-
st.
|
7 |
-
st.write("This application was developed by Daniel Rosehill. Its purpose is to help the user to explore the relationship between companies profitability and their greenhouse gas emissions. Greenhouse gas emissions are monetized at the rate of 236 per ton of carbon dioxide equivalents as proposed in 2023 by the International Foundation for valuing impacts based upon an extensive consultation process informed by the latest scientific evidence.")
|
8 |
|
9 |
-
# Load data
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
data['total_emissions'] = data['scope_1_emissions'] + data['scope_2_emissions'] + data['scope_3_emissions']
|
15 |
-
data['total_monetized_emissions'] = data['total_emissions'] * 236 / 1000 # in billions of USD
|
16 |
|
17 |
-
#
|
18 |
-
st.sidebar.title("
|
19 |
-
|
20 |
-
industry_selection = st.sidebar.multiselect("Select Industry", sector_options, default=sector_options)
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
company_selection = st.sidebar.multiselect("Select Companies (max 5)", company_options, default=company_options[:5])
|
28 |
-
if len(company_selection) > 5:
|
29 |
-
st.sidebar.warning("Please select up to 5 companies.")
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
# Set page config
|
7 |
+
st.set_page_config(layout="wide", page_title="GHG Emissions Analysis")
|
|
|
8 |
|
9 |
+
# Load data
|
10 |
+
@st.cache_data
|
11 |
+
def load_data():
|
12 |
+
url = "https://raw.githubusercontent.com/danielrosehill/GHG-Emissions-Data-Pipeline/refs/heads/main/company_data.csv"
|
13 |
+
df = pd.read_csv(url)
|
14 |
+
|
15 |
+
# Calculate total emissions and handle NaN values
|
16 |
+
df['scope_1_emissions'] = pd.to_numeric(df['scope_1_emissions'], errors='coerce').fillna(0)
|
17 |
+
df['scope_2_emissions'] = pd.to_numeric(df['scope_2_emissions'], errors='coerce').fillna(0)
|
18 |
+
df['scope_3_emissions'] = pd.to_numeric(df['scope_3_emissions'], errors='coerce').fillna(0)
|
19 |
+
|
20 |
+
df['total_emissions'] = df['scope_1_emissions'] + df['scope_2_emissions'] + df['scope_3_emissions']
|
21 |
+
df['monetized_emissions'] = (df['total_emissions'] * 236) / 1000 # Convert to billions
|
22 |
+
df['monetized_emissions'] = df['monetized_emissions'].round(2)
|
23 |
+
|
24 |
+
# Convert EBITDA to numeric and handle NaN values
|
25 |
+
df['ebitda_2022'] = pd.to_numeric(df['ebitda_2022'], errors='coerce').fillna(0)
|
26 |
+
|
27 |
+
return df
|
28 |
|
29 |
+
df = load_data()
|
|
|
|
|
30 |
|
31 |
+
# Sidebar
|
32 |
+
st.sidebar.title("Selection Options")
|
33 |
+
selection_mode = st.sidebar.radio("Selection Mode", ["Individual Companies", "By Sector"])
|
|
|
34 |
|
35 |
+
if selection_mode == "Individual Companies":
|
36 |
+
selected_companies = st.sidebar.multiselect(
|
37 |
+
"Select Companies (max 5)",
|
38 |
+
options=df['company_name'].dropna().unique(),
|
39 |
+
max_selections=5
|
40 |
+
)
|
41 |
+
filtered_df = df[df['company_name'].isin(selected_companies)]
|
42 |
+
else:
|
43 |
+
selected_sector = st.sidebar.multiselect(
|
44 |
+
"Select Sectors",
|
45 |
+
options=df['sector'].dropna().unique()
|
46 |
+
)
|
47 |
+
filtered_df = df[df['sector'].isin(selected_sector)]
|
48 |
|
49 |
+
# Main content
|
50 |
+
st.title("Greenhouse Gas Emissions vs Financial Performance")
|
|
|
|
|
|
|
51 |
|
52 |
+
if not filtered_df.empty:
|
53 |
+
# Create visualization
|
54 |
+
fig = go.Figure()
|
55 |
+
|
56 |
+
# Add EBITDA bars
|
57 |
+
fig.add_trace(go.Bar(
|
58 |
+
x=filtered_df['company_name'],
|
59 |
+
y=filtered_df['ebitda_2022'],
|
60 |
+
name='EBITDA',
|
61 |
+
marker_color='green'
|
62 |
+
))
|
63 |
+
|
64 |
+
# Add monetized emissions bars
|
65 |
+
fig.add_trace(go.Bar(
|
66 |
+
x=filtered_df['company_name'],
|
67 |
+
y=-filtered_df['monetized_emissions'],
|
68 |
+
name='Monetized Emissions',
|
69 |
+
marker_color='red'
|
70 |
+
))
|
71 |
+
|
72 |
+
fig.update_layout(
|
73 |
+
barmode='relative',
|
74 |
+
title='EBITDA vs Monetized Emissions (Billions USD)',
|
75 |
+
yaxis_title='Billions USD',
|
76 |
+
height=600,
|
77 |
+
showlegend=True,
|
78 |
+
xaxis_tickangle=-45
|
79 |
+
)
|
80 |
+
|
81 |
+
st.plotly_chart(fig, use_container_width=True)
|
82 |
+
|
83 |
+
# Calculate correlation
|
84 |
+
valid_data = filtered_df[['ebitda_2022', 'monetized_emissions']].dropna()
|
85 |
+
if len(valid_data) > 1: # Need at least 2 points for correlation
|
86 |
+
correlation = np.corrcoef(valid_data['ebitda_2022'], valid_data['monetized_emissions'])[0,1]
|
87 |
+
st.write(f"Correlation between EBITDA and Monetized Emissions: {correlation:.2f}")
|
88 |
+
|
89 |
+
# Display data table
|
90 |
+
st.subheader("Data Table")
|
91 |
+
display_df = filtered_df[['company_name', 'sector', 'ebitda_2022', 'monetized_emissions']]
|
92 |
+
display_df.columns = ['Company', 'Sector', 'EBITDA (Billions USD)', 'Monetized Emissions (Billions USD)']
|
93 |
+
st.dataframe(display_df)
|
94 |
+
else:
|
95 |
+
st.write("Please select companies or sectors to visualize data")
|