import pandas as pd
import plotly.express as px
from dash import Dash, dcc, html, Input, Output
df = pd.read_csv('https://py.cafe/files/Feanor1992/Green Climate Fund/ODL-Export-Countries.csv')
# Convert columns to numeric
df['RP Financing $'] = pd.to_numeric(df['RP Financing $'], errors='coerce')
df['FA Financing $'] = pd.to_numeric(df['FA Financing $'], errors='coerce')
# Handle missing values in the financing columns
df['RP Financing $'] = df['RP Financing $'].fillna(0)
df['FA Financing $'] = df['FA Financing $'].fillna(0)
# Create a new column for total financing
df['Total Financing $'] = df['RP Financing $'] + df['FA Financing $']
df['Total Financing $'] = pd.to_numeric(df['Total Financing $'])
# Analyze data by region
region_summary = df.groupby('Region').agg(
total_financing=('Total Financing $', 'sum'),
avg_financing=('Total Financing $', 'mean'),
num_countries=('Country Name', 'count')
).reset_index()
region_summary['total_financing'] = pd.to_numeric(region_summary['total_financing'])
region_summary['avg_financing'] = pd.to_numeric(region_summary['avg_financing'])
# Analyze SIDS and LDCs
sids_ldcs_summary = df.groupby(['SIDS', 'LDCs']).agg(
total_financing=('Total Financing $', 'sum'),
avg_financing=('Total Financing $', 'mean'),
num_countries=('Country Name', 'count')
).reset_index()
sids_ldcs_summary['total_financing'] = pd.to_numeric(sids_ldcs_summary['total_financing'])
sids_ldcs_summary['avg_financing'] = pd.to_numeric(sids_ldcs_summary['avg_financing'])
# Identify top countries by total financing
top_countries = df.nlargest(
10,
'Total Financing $'
)[['Country Name', 'Total Financing $']]
top_countries['Total Financing $'] = pd.to_numeric(top_countries['Total Financing $'])
# Analyze financing distribution
financing_distribution = df[['RP Financing $', 'FA Financing $']].describe().T
financing_distribution = financing_distribution.round(2)
# Create a Dash app
app = Dash(__name__)
app.title = "Green Climate Analysis"
# Choropleth Map
choropleth_map = px.choropleth(
data_frame=df,
locations='ISO3',
color='Total Financing $',
color_continuous_scale='Mint',
hover_name='Country Name',
hover_data={
'Region': True,
'SIDS': True,
'LDCs': True,
'RP Financing $': ':.2f',
'FA Financing $': True,
'Total Financing $': ':.2f'
},
title='Total Financing by Country',
projection='orthographic'
)
choropleth_map.update_layout(
template="plotly_dark",
font=dict(color="white"),
title_font_size=20,
paper_bgcolor="rgba(0, 0, 0, 0)",
geo_bgcolor="rgba(0, 0, 0, 0)"
)
# Financing Distribution
financing_distribution_chart = px.bar(
df,
x='Country Name',
y=['RP Financing $', 'FA Financing $'],
barmode='group',
title='Financing Distribution'
)
financing_distribution_chart. update_layout(
template='plotly_dark',
font=dict(color='white'),
title_font_size=20,
paper_bgcolor='rgba(0, 0, 0, 0)'
)
# Total Financing by Region
region_bar_chart = px.bar(
region_summary,
x='Region',
y='total_financing',
text='num_countries',
title='Total Financing by Region',
labels={
'total_financing': 'Total Financing $',
'num_countries': 'Number of Countries'
},
color='avg_financing',
color_continuous_scale='plasma'
)
region_bar_chart.update_traces(
texttemplate='%{text} countries',
textposition='outside'
)
region_bar_chart.update_layout(
template="plotly_dark",
xaxis_title='Region',
yaxis_title='Total Financing $'
)
# SIDS and LDCs Bar Chart
sids_ldcs_bar_chart = px.bar(
sids_ldcs_summary,
x=['SIDS', 'LDCs'],
y='total_financing',
color='num_countries',
title='SIDS and LDCs Financing'
)
sids_ldcs_bar_chart.update_layout(
template='plotly_dark',
font=dict(color='white'),
title_font_size=20,
paper_bgcolor='rgba(0, 0, 0, 0)'
)
# RP vs FA Financing
rp_vs_fa_scatter = px.scatter(
df,
x="RP Financing $",
y="FA Financing $",
size="Total Financing $",
color="Region",
title="RP Financing vs FA Financing"
)
rp_vs_fa_scatter.update_layout(
template="plotly_dark",
font=dict(color="white"),
title_font_size=20,
paper_bgcolor="rgba(0, 0, 0, 0)",
)
# Layout
app.layout = html.Div([
dcc.Tabs([
dcc.Tab(label="Map & Distribution", children=[
html.Div([
html.P(
"RP Financing ($) represents readiness program funding, while FA Financing ($) represents funding approvals for projects.",
style={"color": "white", "fontSize": "16px"}
),
html.Div([
dcc.Graph(figure=choropleth_map, style={"width": "48%", "display": "inline-block"}),
dcc.Graph(figure=financing_distribution_chart, style={"width": "48%", "display": "inline-block"})
])
])
]),
dcc.Tab(label="SIDS and LDCs", children=[
html.Div([
html.P(
"SIDS (Small Island Developing States) and LDCs (Least Developed Countries) are vulnerable to climate change and require financial support to address these challenges.",
style={"color": "white", "fontSize": "16px"}
),
dcc.Graph(figure=sids_ldcs_bar_chart)
])
]),
dcc.Tab(label="RP vs FA Analysis", children=[
html.Div([
dcc.Graph(figure=rp_vs_fa_scatter)
])
]),
dcc.Tab(label="Regional Analysis", children=[
html.Div([
dcc.Graph(figure=region_bar_chart)
])
])
])
], style={"backgroundColor": "black", "padding": "10px"})
if __name__ == '__main__':
app.run_server(debug=True)