Skip to content

Commit

Permalink
Merge pull request #3 from ONEcampaign/analysis
Browse files Browse the repository at this point in the history
Analysis
  • Loading branch information
lpicci96 authored Oct 25, 2024
2 parents dfd1c50 + cb20054 commit 44b25ae
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 0 deletions.
9 changes: 9 additions & 0 deletions output/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Data for the analysis

- `climate_finance_commitments.csv` contains the climate finance commitments data for all climate finance providers between 2010 and 2022.
- `fossil_fuel_subsidies.csv` contains the fossil fuel subsidies data for all countries between 2010 and 2022.
- `chart_1_data.csv` contains the data for the main chart in the page which includes climate finance commitments and fossil fuel subsidies aggregated for all high income countries between 2010 and 2022.
- `chart_2_data.csv` contains the data for the second chart in the page which includes climate finance commitments and fossil fuel subsidies for specified countries between 2010 and 2022.



14 changes: 14 additions & 0 deletions output/chart_1_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
year,climate_finance_commitments,fossil_fuel_subsidies,currency
2010,21893467055.0,186469445295.0,US$ current
2011,17292535537.0,208527172264.0,US$ current
2012,23028812044.515625,222499879183.0,US$ current
2013,26145721949.65625,236866523516.0,US$ current
2014,29770939758.585938,223349743589.0,US$ current
2015,31128861542.765625,170384642882.0,US$ current
2016,31782042354.366943,141416390209.0,US$ current
2017,31966082393.685547,152602715341.0,US$ current
2018,34202588128.68457,161218056759.0,US$ current
2019,36693368107.251465,160628302990.0,US$ current
2020,48760260386.07495,140457603910.0,US$ current
2021,45661184763.576675,214559259725.0,US$ current
2022,59094904942.00781,489322710016.0,US$ current
92 changes: 92 additions & 0 deletions output/chart_2_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
year,iso3_code,climate_finance_commitments,fossil_fuel_subsidies,currency,country_name
2010,BEL,191748248.0,2832889000.0,US$ current,Belgium
2011,BEL,245891915.0,2862083000.0,US$ current,Belgium
2012,BEL,102789892.0,2686533000.0,US$ current,Belgium
2013,BEL,356799880.0,3018475000.0,US$ current,Belgium
2014,BEL,495135712.0,2561219000.0,US$ current,Belgium
2015,BEL,365394632.0,2455802000.0,US$ current,Belgium
2016,BEL,302240624.0,2583381000.0,US$ current,Belgium
2017,BEL,396248620.0,2961450000.0,US$ current,Belgium
2018,BEL,360204336.0,3351782000.0,US$ current,Belgium
2019,BEL,464960384.0,3548509000.0,US$ current,Belgium
2020,BEL,459253120.0,3921943000.0,US$ current,Belgium
2021,BEL,445219520.0,3893607000.0,US$ current,Belgium
2022,BEL,422986816.0,7746767000.0,US$ current,Belgium
2010,CAN,95947242.0,4032754000.0,US$ current,Canada
2011,CAN,549096155.0,2684936000.0,US$ current,Canada
2012,CAN,784711872.0,2889677000.0,US$ current,Canada
2013,CAN,478913328.0,3032590000.0,US$ current,Canada
2014,CAN,284722656.0,3125070000.0,US$ current,Canada
2015,CAN,483636592.0,1980783000.0,US$ current,Canada
2016,CAN,894131312.0,2224104000.0,US$ current,Canada
2017,CAN,555750832.0,2462430000.0,US$ current,Canada
2018,CAN,837664896.0,2225794000.0,US$ current,Canada
2019,CAN,883300448.0,1931571000.0,US$ current,Canada
2020,CAN,748377660.0,3569831000.0,US$ current,Canada
2021,CAN,904346928.0,3232945000.0,US$ current,Canada
2022,CAN,1947453728.0,4428242000.0,US$ current,Canada
2010,DEU,3476314014.0,15752273000.0,US$ current,Germany
2011,DEU,4599120782.0,15032699000.0,US$ current,Germany
2012,DEU,3673832720.0,13731155000.0,US$ current,Germany
2013,DEU,4460819488.0,13264537000.0,US$ current,Germany
2014,DEU,7796556000.0,13723932000.0,US$ current,Germany
2015,DEU,5629361952.0,9808038000.0,US$ current,Germany
2016,DEU,7378621056.0,10200632000.0,US$ current,Germany
2017,DEU,7343885120.0,9574237000.0,US$ current,Germany
2018,DEU,8840272384.0,8891961000.0,US$ current,Germany
2019,DEU,8692651136.0,8755082000.0,US$ current,Germany
2020,DEU,8971566592.0,9475677000.0,US$ current,Germany
2021,DEU,10556995968.0,7889734000.0,US$ current,Germany
2022,DEU,13488980864.0,21401121000.0,US$ current,Germany
2010,FRA,3196592520.0,3605735000.0,US$ current,France
2011,FRA,1972004463.0,5918445000.0,US$ current,France
2012,FRA,4751460233.0,6048392000.0,US$ current,France
2013,FRA,2765924512.0,6129320000.0,US$ current,France
2014,FRA,3339349984.0,5914809000.0,US$ current,France
2015,FRA,4020407616.0,5590883000.0,US$ current,France
2016,FRA,3384218624.0,6227395000.0,US$ current,France
2017,FRA,5211286592.0,7346174000.0,US$ current,France
2018,FRA,2763740224.0,10303833000.0,US$ current,France
2019,FRA,6060038784.0,10000982000.0,US$ current,France
2020,FRA,7904167648.0,7603994000.0,US$ current,France
2021,FRA,8503958344.0,10515700000.0,US$ current,France
2022,FRA,9574580480.0,29207327804.0,US$ current,France
2010,GBR,1095766650.0,10092086000.0,US$ current,United Kingdom
2011,GBR,390532198.0,8489527000.0,US$ current,United Kingdom
2012,GBR,779734688.0,10408687000.0,US$ current,United Kingdom
2013,GBR,1671452000.0,27156066000.0,US$ current,United Kingdom
2014,GBR,2114828768.0,25158376000.0,US$ current,United Kingdom
2015,GBR,3417216128.0,20759857000.0,US$ current,United Kingdom
2016,GBR,2188959872.0,15147421000.0,US$ current,United Kingdom
2017,GBR,2229768384.0,15090442000.0,US$ current,United Kingdom
2018,GBR,2114432032.0,15693130000.0,US$ current,United Kingdom
2019,GBR,2621597248.0,15326139000.0,US$ current,United Kingdom
2020,GBR,3203645232.0,13698526000.0,US$ current,United Kingdom
2021,GBR,2261871744.0,18920555000.0,US$ current,United Kingdom
2022,GBR,2602125696.0,50207640853.0,US$ current,United Kingdom
2010,ITA,39707138.0,11081615000.0,US$ current,Italy
2011,ITA,96114132.0,11340242000.0,US$ current,Italy
2012,ITA,90192558.5,12670792000.0,US$ current,Italy
2013,ITA,230325168.0,13570932000.0,US$ current,Italy
2014,ITA,254199420.0,13026661000.0,US$ current,Italy
2015,ITA,404235392.0,10006324000.0,US$ current,Italy
2016,ITA,222025076.0,9745937000.0,US$ current,Italy
2017,ITA,620194784.0,10018464000.0,US$ current,Italy
2018,ITA,438735928.0,11144166000.0,US$ current,Italy
2019,ITA,503053184.0,11277775000.0,US$ current,Italy
2020,ITA,894737984.0,10791698000.0,US$ current,Italy
2021,ITA,900554816.0,16136249000.0,US$ current,Italy
2022,ITA,583962288.0,50112338000.0,US$ current,Italy
2010,USA,827844730.0,15053324000.0,US$ current,United States
2011,USA,1121416380.0,11614896000.0,US$ current,United States
2012,USA,1498282048.0,11727048000.0,US$ current,United States
2013,USA,2536570256.0,11287730000.0,US$ current,United States
2014,USA,2142482464.0,11465460000.0,US$ current,United States
2015,USA,1761292048.0,10868947000.0,US$ current,United States
2016,USA,2972719872.0,9276425000.0,US$ current,United States
2017,USA,2191163712.0,9300564000.0,US$ current,United States
2018,USA,1518862528.0,10452906000.0,US$ current,United States
2019,USA,2081827072.0,11573847000.0,US$ current,United States
2020,USA,2329149312.0,10308295000.0,US$ current,United States
2021,USA,2450758720.0,12206586000.0,US$ current,United States
2022,USA,5870904064.0,14032668000.0,US$ current,United States
91 changes: 91 additions & 0 deletions scripts/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Module to run the analysis and create the charts"""

import pandas as pd

from scripts.config import Paths
from scripts.logger import logger
from scripts.utils import convert_entities

CF = pd.read_csv(
Paths.output / "climate_finance_commitments.csv"
) # climate finance data
FFS = pd.read_csv(
Paths.output / "fossil_fuel_subsidies.csv"
) # fossil fuel subsidies data

small_multiple_countries = ["USA", "FRA", "DEU", "GBR", "ITA", "CAN", "BEL"]


def cf_high_income_agg() -> pd.DataFrame:
"""Aggregate the climate finance commitments for high-income countries"""

return (
CF.loc[lambda d: d.income_level == "High income"]
.groupby(["year"])
.agg({"value": "sum"})
.reset_index()
)


def ffs_high_income_agg() -> pd.DataFrame:
"""Aggregate the fossil fuel subsidies for high-income countries"""

return (
FFS.loc[lambda d: d.income_level == "High income"]
.groupby(["year"])
.agg({"value": "sum"})
.reset_index()
)


def chart_1_data() -> pd.DataFrame:
"""create data for chart 1: Aggregate Climate finance commitments vs fossil fuel subsidies for high income countries"""

cf = cf_high_income_agg().rename(columns={"value": "climate_finance_commitments"})
ffs = ffs_high_income_agg().rename(columns={"value": "fossil_fuel_subsidies"})

return cf.merge(ffs, on="year", how="outer").assign(currency="US$ current")


def chart_2_data():
"""create data for chart 2: small multiple of Climate finance commitments vs fossil fuel subsidies for specified countries"""

cf = (
CF.loc[
lambda d: d.iso3_code.isin(small_multiple_countries),
["year", "iso3_code", "value"],
]
.rename(columns={"value": "climate_finance_commitments"})
.reset_index(drop=True)
)

ffs = (
FFS.loc[
lambda d: d.iso3_code.isin(small_multiple_countries),
["year", "iso3_code", "value"],
]
.rename(columns={"value": "fossil_fuel_subsidies"})
.reset_index(drop=True)
)

return (
pd.merge(cf, ffs, how="outer")
.sort_values(["iso3_code", "year"])
.assign(
currency="US$ current",
country_name=lambda d: convert_entities(
d.iso3_code, from_type="ISO3", to_type="name_short"
),
)
.reset_index(drop=True)
)


if __name__ == "__main__":
chart_1_data().to_csv(Paths.output / "chart_1_data.csv", index=False)
logger.info("Chart 1 data successfully created and saved.")

chart_2_data().to_csv(Paths.output / "chart_2_data.csv", index=False)
logger.info("Chart 2 data successfully created and saved.")

logger.info("Analysis completed.")
94 changes: 94 additions & 0 deletions scripts/charts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""Module to format the data for flourish charts"""

import pandas as pd

from scripts.config import Paths

chart_1_data = pd.read_csv(Paths.output / "chart_1_data.csv")
chart_2_data = pd.read_csv(Paths.output / "chart_2_data.csv")


def _add_max(df, column):
"""Add the value for latest year for each country to the data"""

max_dict = df.loc[df.groupby(["country_name"])["year"].idxmax()].set_index(
"country_name"
)[column]

return df.assign(max_value=df.country_name.map(max_dict))


def format_to_billion(value, decimals=1):
"""format value to billion"""

value_in_billion = value / 1e9

# if decimals is more than 0
# if the remainer is 0, then format to integer
if decimals > 0 and value_in_billion % 1 == 0:
return f"${value_in_billion:.0f} billion"

return f"${value_in_billion:.{decimals}f} billion"


def create_chart_2() -> pd.DataFrame:
"""Create data for chart 2"""

_ffs = (
chart_2_data.loc[:, ["year", "country_name", "fossil_fuel_subsidies"]]
.pipe(_add_max, "fossil_fuel_subsidies")
.assign(fossil_fuel_subsidies=lambda d: d.fossil_fuel_subsidies * -1)
.assign(max_value=lambda d: d.max_value.apply(format_to_billion))
.pivot(
index=["country_name", "year"],
columns="max_value",
values="fossil_fuel_subsidies",
)
.reset_index()
.assign(indicator="fossil_fuel_subsidies")
)

_cf = (
chart_2_data.loc[:, ["year", "country_name", "climate_finance_commitments"]]
.pipe(_add_max, "climate_finance_commitments")
.assign(max_value=lambda d: d.max_value.apply(format_to_billion))
.pivot(
index=["country_name", "year"],
columns="max_value",
values="climate_finance_commitments",
)
.reset_index()
.assign(indicator="climate_finance_commitments")
)

return pd.concat([_ffs, _cf], ignore_index=True)


def create_chart_1() -> pd.DataFrame:
"""Create data for chart 1"""

return (
chart_1_data.drop(columns="currency")
.rename(
columns={
"climate_finance_commitments": "climate finance commitments",
"fossil_fuel_subsidies": "fossil fuel subsidies",
}
)
.melt(id_vars="year")
.assign(
max_value=lambda d: d.variable.map(
d.loc[d.groupby(["variable"])["year"].idxmax()].set_index("variable")[
"value"
]
)
)
.assign(max_value=lambda d: d.max_value.apply(format_to_billion, decimals=0))
.assign(
value=lambda d: d.value.where(
d.variable != "fossil fuel subsidies", -d.value
)
)
.pivot(index=["year", "variable"], columns="max_value", values="value")
.reset_index()
)

0 comments on commit 44b25ae

Please sign in to comment.