26  Desplazamiento de datos

import pandas as pd
import plotly.express as px
f = '../../data/owid-co2-data.csv'
all_co2 = pd.read_csv(f)
all_co2.columns
Index(['country', 'year', 'iso_code', 'population', 'gdp', 'cement_co2',
       'cement_co2_per_capita', 'co2', 'co2_growth_abs', 'co2_growth_prct',
       'co2_including_luc', 'co2_including_luc_growth_abs',
       'co2_including_luc_growth_prct', 'co2_including_luc_per_capita',
       'co2_including_luc_per_gdp', 'co2_including_luc_per_unit_energy',
       'co2_per_capita', 'co2_per_gdp', 'co2_per_unit_energy', 'coal_co2',
       'coal_co2_per_capita', 'consumption_co2', 'consumption_co2_per_capita',
       'consumption_co2_per_gdp', 'cumulative_cement_co2', 'cumulative_co2',
       'cumulative_co2_including_luc', 'cumulative_coal_co2',
       'cumulative_flaring_co2', 'cumulative_gas_co2', 'cumulative_luc_co2',
       'cumulative_oil_co2', 'cumulative_other_co2', 'energy_per_capita',
       'energy_per_gdp', 'flaring_co2', 'flaring_co2_per_capita', 'gas_co2',
       'gas_co2_per_capita', 'ghg_excluding_lucf_per_capita', 'ghg_per_capita',
       'land_use_change_co2', 'land_use_change_co2_per_capita', 'methane',
       'methane_per_capita', 'nitrous_oxide', 'nitrous_oxide_per_capita',
       'oil_co2', 'oil_co2_per_capita', 'other_co2_per_capita',
       'other_industry_co2', 'primary_energy_consumption',
       'share_global_cement_co2', 'share_global_co2',
       'share_global_co2_including_luc', 'share_global_coal_co2',
       'share_global_cumulative_cement_co2', 'share_global_cumulative_co2',
       'share_global_cumulative_co2_including_luc',
       'share_global_cumulative_coal_co2',
       'share_global_cumulative_flaring_co2',
       'share_global_cumulative_gas_co2', 'share_global_cumulative_luc_co2',
       'share_global_cumulative_oil_co2', 'share_global_cumulative_other_co2',
       'share_global_flaring_co2', 'share_global_gas_co2',
       'share_global_luc_co2', 'share_global_oil_co2',
       'share_global_other_co2', 'share_of_temperature_change_from_ghg',
       'temperature_change_from_ch4', 'temperature_change_from_co2',
       'temperature_change_from_ghg', 'temperature_change_from_n2o',
       'total_ghg', 'total_ghg_excluding_lucf', 'trade_co2',
       'trade_co2_share'],
      dtype='object')
all_co2.iso_code.unique()
array(['AFG', nan, 'ALB', 'DZA', 'AND', 'AGO', 'AIA', 'ATA', 'ATG', 'ARG',
       'ARM', 'ABW', 'AUS', 'AUT', 'AZE', 'BHS', 'BHR', 'BGD', 'BRB',
       'BLR', 'BEL', 'BLZ', 'BEN', 'BMU', 'BTN', 'BOL', 'BES', 'BIH',
       'BWA', 'BRA', 'VGB', 'BRN', 'BGR', 'BFA', 'BDI', 'KHM', 'CMR',
       'CAN', 'CPV', 'CAF', 'TCD', 'CHL', 'CHN', 'CXR', 'COL', 'COM',
       'COG', 'COK', 'CRI', 'CIV', 'HRV', 'CUB', 'CUW', 'CYP', 'CZE',
       'COD', 'DNK', 'DJI', 'DMA', 'DOM', 'TLS', 'ECU', 'EGY', 'SLV',
       'GNQ', 'ERI', 'EST', 'SWZ', 'ETH', 'FRO', 'FJI', 'FIN', 'FRA',
       'PYF', 'GAB', 'GMB', 'GEO', 'DEU', 'GHA', 'GRC', 'GRL', 'GRD',
       'GTM', 'GIN', 'GNB', 'GUY', 'HTI', 'HND', 'HKG', 'HUN', 'ISL',
       'IND', 'IDN', 'IRN', 'IRQ', 'IRL', 'ISR', 'ITA', 'JAM', 'JPN',
       'JOR', 'KAZ', 'KEN', 'KIR', 'KWT', 'KGZ', 'LAO', 'LVA', 'LBN',
       'LSO', 'LBR', 'LBY', 'LIE', 'LTU', 'LUX', 'MAC', 'MDG', 'MWI',
       'MYS', 'MDV', 'MLI', 'MLT', 'MHL', 'MRT', 'MUS', 'MEX', 'FSM',
       'MDA', 'MCO', 'MNG', 'MNE', 'MSR', 'MAR', 'MOZ', 'MMR', 'NAM',
       'NRU', 'NPL', 'NLD', 'NCL', 'NZL', 'NIC', 'NER', 'NGA', 'NIU',
       'PRK', 'MKD', 'NOR', 'OMN', 'PAK', 'PLW', 'PSE', 'PAN', 'PNG',
       'PRY', 'PER', 'PHL', 'POL', 'PRT', 'QAT', 'ROU', 'RUS', 'RWA',
       'SHN', 'KNA', 'LCA', 'SPM', 'VCT', 'WSM', 'SMR', 'STP', 'SAU',
       'SEN', 'SRB', 'SYC', 'SLE', 'SGP', 'SXM', 'SVK', 'SVN', 'SLB',
       'SOM', 'ZAF', 'KOR', 'SSD', 'ESP', 'LKA', 'SDN', 'SUR', 'SWE',
       'CHE', 'SYR', 'TWN', 'TJK', 'TZA', 'THA', 'TGO', 'TON', 'TTO',
       'TUN', 'TUR', 'TKM', 'TCA', 'TUV', 'UGA', 'UKR', 'ARE', 'GBR',
       'USA', 'URY', 'UZB', 'VUT', 'VAT', 'VEN', 'VNM', 'WLF', 'YEM',
       'ZMB', 'ZWE'], dtype=object)
mascara_mex = all_co2['iso_code']=='MEX'
columnas = ['co2','year','co2_growth_prct']

co2_mex = all_co2[mascara_mex][columnas]


co2_mex
co2 year co2_growth_prct
27144 NaN 1850 NaN
27145 NaN 1851 NaN
27146 NaN 1852 NaN
27147 NaN 1853 NaN
27148 NaN 1854 NaN
... ... ... ...
27312 470.286 2018 1.002
27313 468.322 2019 -0.418
27314 442.289 2020 -5.559
27315 468.791 2021 5.992
27316 511.972 2022 9.211

173 rows × 3 columns

co2_mex.sort_values(by='year',ascending=True,inplace=True)
co2_mex
co2 year co2_growth_prct
27144 NaN 1850 NaN
27145 NaN 1851 NaN
27146 NaN 1852 NaN
27147 NaN 1853 NaN
27148 NaN 1854 NaN
... ... ... ...
27312 470.286 2018 1.002
27313 468.322 2019 -0.418
27314 442.289 2020 -5.559
27315 468.791 2021 5.992
27316 511.972 2022 9.211

173 rows × 3 columns

co2_mex['pct_change'] = co2_mex['co2'].pct_change() * 100
co2_mex
co2 year co2_growth_prct pct_change
27144 NaN 1850 NaN NaN
27145 NaN 1851 NaN NaN
27146 NaN 1852 NaN NaN
27147 NaN 1853 NaN NaN
27148 NaN 1854 NaN NaN
... ... ... ... ...
27312 470.286 2018 1.002 1.001888
27313 468.322 2019 -0.418 -0.417618
27314 442.289 2020 -5.559 -5.558782
27315 468.791 2021 5.992 5.992010
27316 511.972 2022 9.211 9.211141

173 rows × 4 columns

px.scatter(co2_mex,x='year',y=['pct_change','co2_growth_prct'])
co2_mex['co2_diff'] =  co2_mex.co2 - co2_mex['co2'].shift(1)
co2_mex
co2 year co2_growth_prct pct_change co2_diff
27144 NaN 1850 NaN NaN NaN
27145 NaN 1851 NaN NaN NaN
27146 NaN 1852 NaN NaN NaN
27147 NaN 1853 NaN NaN NaN
27148 NaN 1854 NaN NaN NaN
... ... ... ... ... ...
27312 470.286 2018 1.002 1.001888 4.665
27313 468.322 2019 -0.418 -0.417618 -1.964
27314 442.289 2020 -5.559 -5.558782 -26.033
27315 468.791 2021 5.992 5.992010 26.502
27316 511.972 2022 9.211 9.211141 43.181

173 rows × 5 columns

px.scatter(co2_mex,x='year',y=['co2_diff'])
px.scatter(x=co2_mex['year'],y=co2_mex['co2'].diff()) # revisar argumentos ed diff