danielrosehill commited on
Commit
995d03d
·
1 Parent(s): 2aacdd5
Files changed (1) hide show
  1. app.py +86 -56
app.py CHANGED
@@ -1,65 +1,95 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
 
4
 
5
- # App title and description
6
- st.title("Monetize GHG Emissions Explorer")
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 from GitHub URL
10
- url = "https://raw.githubusercontent.com/danielrosehill/GHG-Emissions-Data-Pipeline/refs/heads/main/company_data.csv"
11
- data = pd.read_csv(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Process data
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
- # Create sidebar for user input
18
- st.sidebar.title("Filters")
19
- sector_options = data['sector'].unique().tolist()
20
- industry_selection = st.sidebar.multiselect("Select Industry", sector_options, default=sector_options)
21
 
22
- # Filter data based on user selection
23
- filtered_data = data[data['sector'].isin(industry_selection)]
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Allow user to select up to 5 individual companies
26
- company_options = filtered_data['company_name'].unique().tolist()
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
- # Further filter data based on company selection
32
- final_data = filtered_data[filtered_data['company_name'].isin(company_selection)]
33
-
34
- # Display data table
35
- st.write("### Data Table")
36
- st.dataframe(final_data[['company_name', 'ebitda_2022', 'total_monetized_emissions']])
37
-
38
- # Plot bar chart
39
- fig, ax = plt.subplots(figsize=(12, 6))
40
- bar_width = 0.35
41
- index = range(len(final_data))
42
-
43
- # Plot profitability (positive values)
44
- bars1 = ax.bar(index, final_data['ebitda_2022'], bar_width, label='Profitability', color='green')
45
-
46
- # Plot monetized emissions (negative values)
47
- bars2 = ax.bar([p + bar_width for p in index], -final_data['total_monetized_emissions'], bar_width, label='Monetized Emissions', color='red')
48
-
49
- # Add labels and title
50
- ax.set_xlabel('Company')
51
- ax.set_ylabel('Values')
52
- ax.set_title('Profitability vs Monetized Emissions')
53
- ax.set_xticks([p + bar_width / 2 for p in index])
54
- ax.set_xticklabels(final_data['company_name'], rotation=45, ha='right')
55
- ax.legend()
56
-
57
- # Display the plot
58
- st.write("### Visualization")
59
- st.pyplot(fig)
60
-
61
- # Calculate and display correlation
62
- correlation = final_data['ebitda_2022'].corr(final_data['total_monetized_emissions'])
63
- st.write("### Correlation Calculation")
64
- st.write(f"The correlation between profitability and monetized emissions is: {correlation:.2f}")
65
- st.write("A negative correlation indicates that companies are less profitable if they emit more.")
 
 
 
 
 
 
 
 
 
 
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")