From 5cd6bae39dcb4de6d90f55736d5ae62bdf6fe05e Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 15:13:40 +0200 Subject: [PATCH 1/9] create chart data and script --- output/chart_1_data.csv | 14 +++++++++++++ scripts/analysis.py | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 output/chart_1_data.csv create mode 100644 scripts/analysis.py diff --git a/output/chart_1_data.csv b/output/chart_1_data.csv new file mode 100644 index 0000000..69e7dd0 --- /dev/null +++ b/output/chart_1_data.csv @@ -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 diff --git a/scripts/analysis.py b/scripts/analysis.py new file mode 100644 index 0000000..06fd2fd --- /dev/null +++ b/scripts/analysis.py @@ -0,0 +1,44 @@ +"""Module to run the analysis and create the charts""" + +import pandas as pd + +from scripts.config import Paths +from scripts.logger import logger + +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 + +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') + + +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.") + From d9013ef23e3e9473b4e0fb630b441068b802d776 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 15:23:18 +0200 Subject: [PATCH 2/9] add readme --- output/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/output/README.md b/output/README.md index e69de29..86cdc89 100644 --- a/output/README.md +++ b/output/README.md @@ -0,0 +1,5 @@ +# 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. \ No newline at end of file From 798397ff1c9869f5207112243c603050e70d8359 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 15:23:37 +0200 Subject: [PATCH 3/9] black --- scripts/analysis.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/scripts/analysis.py b/scripts/analysis.py index 06fd2fd..808605a 100644 --- a/scripts/analysis.py +++ b/scripts/analysis.py @@ -5,40 +5,45 @@ from scripts.config import Paths from scripts.logger import logger -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 +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 + 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() - ) + 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() - ) + 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'}) + 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') + return cf.merge(ffs, on="year", how="outer").assign(currency="US$ current") -if __name__ == '__main__': +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.") - From 0ed6a9104a704147ff2d4ac01a2a92e78758521b Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 15:41:53 +0200 Subject: [PATCH 4/9] create small multiple data --- output/README.md | 6 +++- output/chart_2_data.csv | 66 +++++++++++++++++++++++++++++++++++++++++ scripts/analysis.py | 33 +++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 output/chart_2_data.csv diff --git a/output/README.md b/output/README.md index 86cdc89..944d8d1 100644 --- a/output/README.md +++ b/output/README.md @@ -2,4 +2,8 @@ - `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. \ No newline at end of file +- `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. + + + diff --git a/output/chart_2_data.csv b/output/chart_2_data.csv new file mode 100644 index 0000000..869afac --- /dev/null +++ b/output/chart_2_data.csv @@ -0,0 +1,66 @@ +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,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 diff --git a/scripts/analysis.py b/scripts/analysis.py index 808605a..3d48f4a 100644 --- a/scripts/analysis.py +++ b/scripts/analysis.py @@ -4,6 +4,7 @@ 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" @@ -12,6 +13,8 @@ Paths.output / "fossil_fuel_subsidies.csv" ) # fossil fuel subsidies data +small_multiple_countries = ['USA', "FR", "GER", "GBR", "ITA", "CAN", "BEL"] + def cf_high_income_agg() -> pd.DataFrame: """Aggregate the climate finance commitments for high-income countries""" @@ -43,7 +46,37 @@ def chart_1_data() -> pd.DataFrame: 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.") From 3a65bf6ce3a0ca17072d0732a8e896934dc4c803 Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 15:42:10 +0200 Subject: [PATCH 5/9] black --- scripts/analysis.py | 51 ++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/scripts/analysis.py b/scripts/analysis.py index 3d48f4a..2df03ae 100644 --- a/scripts/analysis.py +++ b/scripts/analysis.py @@ -13,7 +13,7 @@ Paths.output / "fossil_fuel_subsidies.csv" ) # fossil fuel subsidies data -small_multiple_countries = ['USA', "FR", "GER", "GBR", "ITA", "CAN", "BEL"] +small_multiple_countries = ["USA", "FR", "GER", "GBR", "ITA", "CAN", "BEL"] def cf_high_income_agg() -> pd.DataFrame: @@ -46,30 +46,39 @@ def chart_1_data() -> pd.DataFrame: 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) - ) + 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__": From ad284b6c23aa0976f7b963817485eb7cd7a7509a Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 16:35:19 +0200 Subject: [PATCH 6/9] update --- output/chart_2_data.csv | 26 ++++++++++++++++++++++++++ scripts/analysis.py | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/output/chart_2_data.csv b/output/chart_2_data.csv index 869afac..decadd5 100644 --- a/output/chart_2_data.csv +++ b/output/chart_2_data.csv @@ -25,6 +25,32 @@ year,iso3_code,climate_finance_commitments,fossil_fuel_subsidies,currency,countr 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 diff --git a/scripts/analysis.py b/scripts/analysis.py index 2df03ae..fda676f 100644 --- a/scripts/analysis.py +++ b/scripts/analysis.py @@ -13,7 +13,7 @@ Paths.output / "fossil_fuel_subsidies.csv" ) # fossil fuel subsidies data -small_multiple_countries = ["USA", "FR", "GER", "GBR", "ITA", "CAN", "BEL"] +small_multiple_countries = ["USA", "FRA", "DEU", "GBR", "ITA", "CAN", "BEL"] def cf_high_income_agg() -> pd.DataFrame: From 62fff82ad895a975a3bf7372b89117ffbe9f5a8a Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 16:41:41 +0200 Subject: [PATCH 7/9] add module to create chart data --- scripts/charts.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/charts.py diff --git a/scripts/charts.py b/scripts/charts.py new file mode 100644 index 0000000..ab7d475 --- /dev/null +++ b/scripts/charts.py @@ -0,0 +1,54 @@ +"""Module to format the data for flourish charts""" + +import pandas as pd + +from scripts.config import Paths + +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): + """format value to billion""" + + value_in_billion = value / 1e9 + + # If the value is less than 1 billion, round to 1 decimal place, else round to nearest integer + if value_in_billion < 1: + formatted_value = f"${value_in_billion:.1f} billion" + else: + formatted_value = f"${int(round(value_in_billion))} billion" + + return formatted_value + + +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) \ No newline at end of file From a297d595dfd20dbaf8a12b61c962b9707c04225d Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 17:12:09 +0200 Subject: [PATCH 8/9] add functions to create charts --- scripts/charts.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/scripts/charts.py b/scripts/charts.py index ab7d475..73b9b03 100644 --- a/scripts/charts.py +++ b/scripts/charts.py @@ -4,6 +4,7 @@ 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") @@ -15,18 +16,17 @@ def _add_max(df, column): return df.assign(max_value = df.country_name.map(max_dict)) -def format_to_billion(value): +def format_to_billion(value, decimals = 1): """format value to billion""" value_in_billion = value / 1e9 - # If the value is less than 1 billion, round to 1 decimal place, else round to nearest integer - if value_in_billion < 1: - formatted_value = f"${value_in_billion:.1f} billion" - else: - formatted_value = f"${int(round(value_in_billion))} billion" + # 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 formatted_value + return f"${value_in_billion:.{decimals}f} billion" def create_chart_2() -> pd.DataFrame: @@ -51,4 +51,19 @@ def create_chart_2() -> pd.DataFrame: .assign(indicator = 'climate_finance_commitments') ) - return pd.concat([_ffs, _cf], ignore_index=True) \ No newline at end of file + 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() + + ) \ No newline at end of file From cb20054ae009430b4e4ce7ce545a66ebd149fecb Mon Sep 17 00:00:00 2001 From: Luca Picci Date: Fri, 25 Oct 2024 17:12:25 +0200 Subject: [PATCH 9/9] black --- scripts/charts.py | 89 ++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/scripts/charts.py b/scripts/charts.py index 73b9b03..4bfe15b 100644 --- a/scripts/charts.py +++ b/scripts/charts.py @@ -11,12 +11,14 @@ 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] + 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)) + return df.assign(max_value=df.country_name.map(max_dict)) -def format_to_billion(value, decimals = 1): +def format_to_billion(value, decimals=1): """format value to billion""" value_in_billion = value / 1e9 @@ -32,38 +34,61 @@ def format_to_billion(value, decimals = 1): 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') - ) + _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() - - ) \ No newline at end of file + 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() + )