Continuing with the articles about data science, this time data on global and per-country climate information, as well as CO2 levels, will be analyzed.

I found this data and project on the kaggle (broken link) site, which is a site for publishing data science projects. You can find the data for this article at the following link (broken link).

The code in this article is based on the article on kaggle on the subject, in English: Climate Change and CO2 levels in atmosphere (broken link).

This article explores the global changes in mean temperature and the increase in the concentration of CO2 in the atmosphere.

The data comes from the following files:

  • GlobalLandTemperaturesByCountry.csv: Contains the temperature information by country.
  • GlobalTemperatures.csv: Contains the global temperature information.
  • archive.csv: Contains the information on CO2 levels in the atmosphere.

The notebook is shown below:

https://www.kaggle.com/derevirn/data-analysis-on-climate-change/notebook (broken link) https://www.kaggle.com/derevirn/data-analysis-on-climate-change/data (broken link)

Import the pandas, matplotlib and seaborn libraries.

In [1]:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Create the dataframes from the csv files.

In [2]:

# Se crea el dataframe data_pais, data_vzla, data_global y co2_ppm

data_pais = pd.read_csv("./datos/GlobalLandTemperaturesByCountry.csv")
#Se crea una copia del dataframe obteniendo solo la información de Venezuela.
data_vzla = data_pais[data_pais["Country"] == "Venezuela"].copy()
data_vzla["dt"] = pd.to_datetime(data_vzla["dt"])

#Se crea una copia del dataframe obteniendo solo la información de Argentina.
data_arg = data_pais[data_pais["Country"] == "Argentina"].copy()
data_arg["dt"] = pd.to_datetime(data_arg["dt"])

#Se crea una copia del dataframe obteniendo solo la información de USA.
data_canada = data_pais[data_pais["Country"] == "Canada"].copy()
data_canada["dt"] = pd.to_datetime(data_canada["dt"])

#Se crea una copia del dataframe obteniendo solo la información de Mexico.
data_mexico = data_pais[data_pais["Country"] == "Mexico"].copy()
data_mexico["dt"] = pd.to_datetime(data_mexico["dt"])

#Se crea una copia del dataframe obteniendo solo la información de Brasil.
data_brasil = data_pais[data_pais["Country"] == "Brazil"].copy()
data_brasil["dt"] = pd.to_datetime(data_brasil["dt"])

#Se crea una copia del dataframe obteniendo solo la información de Venezuela.
data_chile = data_pais[data_pais["Country"] == "Chile"].copy()
data_chile["dt"] = pd.to_datetime(data_chile["dt"])

#Se crea el dataframe de la temperatura global. 
data_global = pd.read_csv("./datos/GlobalTemperatures.csv")
#Se modifica la fecha y hora como datetime.
data_global["dt"] = pd.to_datetime(data_global["dt"])
#Se crea el dataframe del co2.
co2_ppm = pd.read_csv("./datos/archive.csv")

The dataframes created.

In [3]:

data_pais.head()

Out[3]:

IDdtAverage TemperatureAverage Temperature UncertaintyCountry
01743-11-014.3842.294Åland
11743-12-01NaNNaNÅland
21744-01-01NaNNaNÅland
31744-02-01NaNNaNÅland
41744-03-01NaNNaNÅland

As can be seen, there is the date, the average temperature, the average temperature uncertainty and the country. In [4]:

data_pais.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 577462 entries, 0 to 577461
Data columns (total 4 columns):
dt                               577462 non-null object
AverageTemperature               544811 non-null float64
AverageTemperatureUncertainty    545550 non-null float64
Country                          577462 non-null object
dtypes: float64(2), object(2)
memory usage: 17.6+ MB

The data types of the columns are: dt is object, average temperature and average temperature uncertainty are float64, and country is object

Venezuela

In [5]:

data_vzla.head()

Out[5]:

IDdtAverage TemperatureAverage Temperature UncertaintyCountry
5623391824-01-0124.6130.824Venezuela
5623401824-02-0124.6702.017Venezuela
5623411824-03-0125.5271.065Venezuela
5623421824-04-0125.7321.771Venezuela
5623431824-05-0124.9991.157Venezuela

This is the same information as above but only for Venezuela. In [6]:

data_vzla.describe().T

Out[6]:

countmeanstdmin25%50%75%max
AverageTemperature2086.025.0256860.70318922.77724.5232524.982525.4952527.807
AverageTemperatureUncertainty2086.00.5860000.3984600.0840.310000.45400.760004.120

There are 2086 data points, for which the average temperature is 25.025 degrees and the uncertainty is 0.586; the standard deviation of the average temperature is 0.703 and of the uncertainty is 0.398; the minimum temperature was 22.77 and the maximum 27.807. In [7]:

data_vzla.corr()

Out[7]:

| | AverageTemperature | AverageTemperatureUncertainty | |:--------------------------- - | :---------------: | :---------------------------: | |AverageTemperature | 1.000000 | -0.319882 | |AverageTemperatureUncertainty | -0.319882 | 1.000000 |

The two variables are, to a certain extent, inversely correlated. Argentina In [8]:

data_arg.head()

Out[8]:

IDdtAverage TemperatureAverage Temperature UncertaintyCountry
254661855-05-0110.7451.499Argentina
254671855-06-016.9952.073Argentina
254681855-07-016.9011.568Argentina
254691855-08-019.0971.412Argentina
254701855-09-0111.3161.384Argentina

In [9]:

data_arg.describe().T

Out[9]:

countmeanstdmin25%50%75%max
AverageTemperature1900.014.6211944.8468854.79610.1322514.630519.2787523.290
AverageTemperatureUncertainty1900.00.5273380.4656520.0830.209000.30350.745252.974

Brazil

In [10]:

data_brasil.head()

Out[10]:

IDdtAverage TemperatureAverage Temperature UncertaintyCountry
771101832-01-0124.9351.372Brazil
771111832-02-0124.5051.953Brazil
771121832-03-0124.6171.359Brazil
771131832-04-0123.9902.013Brazil
771141832-05-0123.1241.592Brazil

In [11]:

data_brasil.describe().T

Out[11]:

countmeanstdmin25%50%75%max
AverageTemperature2164.024.6992560.97878721.79724.0225024.829525.4497527.151
AverageTemperatureUncertainty2164.00.5729400.4117940.0600.243750.41600.840003.353

Chile

In [12]:

data_chile.head()

Out[12]:

IDdtAverageTemperatureAverageTemperatureUncertaintyCountry
1082481855-05-017.1521.296Chile
1082491855-06-014.2571.641Chile
1082501855-07-014.1131.313Chile
1082511855-08-015.5081.168Chile
1082521855-09-016.6841.153Chile

In [13]:

data_chile.describe().T

Out[13]:

countmeanstdmin25%50%75%max
AverageTemperature1900.09.3834743.1790813.2066.5029.381512.3777515.493
AverageTemperatureUncertainty1900.00.5726540.4180110.1140.2810.38250.803002.408

Mexico

In [14]:

data_mexico.head()

Out[14]:

IDdtAverageTemperatureAverageTemperatureUncertaintyCountry
3384381835-01-0115.8101.484Mexico
3384381835-01-0115.8101.484Mexico
3384391835-02-0114.9111.579Mexico
3384401835-03-0116.8261.858Mexico
3384411835-04-0119.9251.966Mexico
3384421835-05-0122.0371.609Mexico

In [15]:

data_mexico.describe().T

Out[15]:

countmeanstdmin25%50%75%max
AverageTemperature2145.020.5675583.75334312.06417.10921.05024.16426.926
AverageTemperatureUncertainty2145.00.6382330.5476050.0910.2260.3980.9703.699

####Canada In [16]:

data_canada.head()

Out[16]:

IDdtAverageTemperatureAverageTemperatureUncertaintyCountry
972551768-09-015.2573.107Canada
972561768-10-01-3.3932.981Canada
972571768-11-01-12.8293.967Canada
972581768-12-01-20.5824.622Canada
972591769-01-01-24.7564.722Canada

In [17]:

data_canada.describe().T

Out[17]:

countmeanstdmin25%50%75%max
AverageTemperature2504.0-5.21665912.878074-28.736-17.7025-4.72857.4127514.796
AverageTemperatureUncertainty2504.01.2562111.0461270.1230.28700.89802.120255.275

Note that the variations in the average temperature uncertainty are greater in Brazil, Venezuela, Mexico and Canada, in that order, compared with Chile and Argentina.

Annual increase in the global average temperature

In [18]:

annual_mean_global = data_global.groupby(data_global["dt"].dt.year).mean()
reference_temperature_global = annual_mean_global.loc[1951:1980].mean()["LandAndOceanAverageTemperature"]
annual_mean_global["Anomaly"] = annual_mean_global["LandAndOceanAverageTemperature"] - reference_temperature_global

In [19]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_global.loc[1960:2015]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Anomalia anual de la temperatura promedio global")
plt.xlabel('')
plt.ylabel('Anomalia de la temperatura')
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-1.png)

Note that the annual temperature increase shows an upward trend that has grown in recent years; this increase in 2015 was 0.75 degrees, which is consistent with climate change.

Venezuela.

The same calculation is done for the case of Venezuela. In [20]:

annual_mean_vzla = data_vzla.groupby(data_vzla["dt"].dt.year).mean()
reference_temperature_vzla = annual_mean_vzla.loc[1951:1980].mean()["AverageTemperature"]
annual_mean_vzla["Anomaly"] = annual_mean_vzla["AverageTemperature"] - reference_temperature_vzla

In [21]:

annual_mean_vzla.head()

Out[21]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
182424.8965451.516091-0.420966
182525.0715831.552083-0.245928
182624.8719001.440800-0.445611
182725.1562731.474182-0.161238
182824.7518181.516727-0.565693

In [22]:

annual_mean_vzla.tail()

Out[22]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
200926.0849170.3539170.767406
201026.1502500.3413330.832739
201125.6773330.3367500.359822
201225.6885830.4014170.371072
201325.9128750.4845000.595364

The last record is from 2013, when the increase was 0.595 degrees; there was a peak in 2010 of 0.8332 degrees. In [23]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_vzla.loc[1960:2012]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Variación promedio anual de la temperatura de Venezuela")
plt.xlabel('')
plt.ylabel('Incremento de la temperatura')
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-2.png)

Argentina

In [24]:

annual_mean_arg = data_arg.groupby(data_arg["dt"].dt.year).mean()
reference_temperature_arg = annual_mean_arg.loc[1951:1980].mean()["AverageTemperature"]
annual_mean_arg["Anomaly"] = annual_mean_arg["AverageTemperature"] - reference_temperature_arg

In [25]:

annual_mean_arg.head()

Out[25]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
185511.7292501.711875-3.090742
185613.9987501.553167-0.821242
185714.2210831.993417-0.598908
185814.0156671.770750-0.804325
185914.4145831.635583-0.405408

In [26]:

annual_mean_arg.tail()

Out[26]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
200915.5344170.2157500.714425
201015.0788330.2189170.258842
201115.3396670.2198330.519675
201215.6890000.2657500.869008
201314.4571250.276500-0.362867

In [27]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_arg.loc[1960:2012]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Variación promedio anual de la temperatura de Argentina")
plt.xlabel('')
plt.ylabel('Incremento de la temperatura')
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-3.png)

In the case of Argentina, the slope of the temperature increase trend is less pronounced.

Brazil

In [28]:

annual_mean_brasil = data_brasil.groupby(data_brasil["dt"].dt.year).mean()
reference_temperature_brasil = annual_mean_brasil.loc[1951:1980].mean()["AverageTemperature"]
annual_mean_brasil["Anomaly"] = annual_mean_brasil["AverageTemperature"] - reference_temperature_brasil

In [29]:

annual_mean_brasil.head()

Out[29]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
183223.8585831.734500-1.072247
183324.5178331.450083-0.412997
183424.3467501.498083-0.584081
183523.5375001.298583-1.393331
183623.9211671.426083-1.009664

In [30]:

annual_mean_brasil.tail()

Out[30]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
200925.6005830.1856670.669753
201025.8124170.1580000.881586
201125.4289170.2048330.498086
201225.7170830.2310000.786253
201325.3481250.2622500.417294

In [31]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_brasil.loc[1960:2012]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Variación promedio anual de la temperatura de Brasil")
plt.xlabel('')
plt.ylabel('Incremento de la temperatura')
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-4.png)

In the case of Brazil, a strong increase is noticeable in the 90s with a peak of more than 1 degree, after which the trend has stabilized at 0.79 degrees.

Chile

In [32]:

annual_mean_chile = data_chile.groupby(data_chile["dt"].dt.year).mean()
reference_temperature_chile = annual_mean_chile.loc[1951:1980].mean()["AverageTemperature"]
annual_mean_chile["Anomaly"] = annual_mean_chile["AverageTemperature"] - reference_temperature_chile

In [33]:

annual_mean_chile.head()

Out[33]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
18557.0701251.424125-2.510742
18568.6640831.319250-0.916783
18578.6459171.439250-0.934950
18588.8102501.428333-0.770617
18599.2455831.376917-0.335283

In [34]:

annual_mean_chile.tail()

Out[34]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
20099.9944170.2819170.413550
20109.7128330.3400000.131967
201110.0322500.3222500.451383
201210.2725830.3845830.691717
20139.8773750.3938750.296508

In [35]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_chile.loc[1960:2012]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Variación promedio anual de la temperatura de Chile")
plt.xlabel('')
plt.ylabel('Incremento de la temperatura')
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-5.png)

The variation does not exceed 0.8 degrees of temperature, with a strong increase between the 70s and 80s and then a peak in the 90s.

Mexico

In [36]:

annual_mean_mexico = data_mexico.groupby(data_mexico["dt"].dt.year).mean()
reference_temperature_mexico = annual_mean_mexico.loc[1951:1980].mean()["AverageTemperature"]
annual_mean_mexico["Anomaly"] = annual_mean_mexico["AverageTemperature"] - reference_temperature_mexico

In [37]:

annual_mean_mexico.head()

Out[37]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
183518.9932501.684083-1.745911
183619.5871671.695333-1.151994
183719.6710831.647667-1.068078
183819.3370001.862417-1.402161
183919.7858331.603000-0.953328

In [38]:

annual_mean_mexico.tail()

Out[38]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
200921.5460830.2059170.806922
201020.8529170.2177500.113756
201121.5993330.2453330.860172
201221.6524170.3245000.913256
201322.2192220.3651111.480061

In [39]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_mexico.loc[1960:2012]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Variación promedio anual de la temperatura de Mexico")
plt.xlabel('')
plt.ylabel('Incremento de la temperatura')
plt.show()


![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-6.png)

Note the upward trend of the temperature in Mexico, reaching 2013 at 1.48 degrees above the average.

####Canada In [40]:

annual_mean_canada = data_canada.groupby(data_canada["dt"].dt.year).mean()
reference_temperature_canada = annual_mean_canada.loc[1951:1980].mean()["AverageTemperature"]
annual_mean_canada["Anomaly"] = annual_mean_canada["AverageTemperature"] - reference_temperature_canada

In [41]:

annual_mean_canada.head()

Out[41]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
1768-7.8867503.66925-2.969253
1769-3.4278753.505501.489622
1770NaNNaNNaN
1771NaNNaNNaN
1772NaNNaNNaN

In [42]:

annual_mean_canada.tail()

Out[42]:

dtAverageTemperatureAverageTemperatureUncertaintyAnomaly
2009-4.1350830.2318330.782414
2010-1.8879170.2185833.029581
2011-3.5570830.2325831.360414
2012-3.1768330.3000831.740664
2013-1.6406670.5336673.276831

In [43]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_mean_canada.loc[1960:2012]["Anomaly"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Variación promedio anual de la temperatura de Canadá")
plt.xlabel('')
plt.ylabel('Incremento de la temperatura')
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-7.png)

Note the upward trend of the temperature in Canada, and the worrying thing is that the variation exceeds 3 degrees in 2013.

Annual CO2 levels in the Atmosphere

In [44]:

plt.figure()
plt.style.use("fivethirtyeight")
annual_co2_ppm = co2_ppm.groupby(co2_ppm["Year"]).mean()
annual_co2_ppm.loc[1960:2015]["Carbon Dioxide (ppm)"].plot(figsize = (10,5), grid=True, legend=True)
plt.title("Nivel anual global de CO2 en la Atmosfera")
plt.ylabel("CO2 partes por millón")
plt.show()

![](/blog/images/analizandodatosdelcambioclimaticoynivelesdeco2enlaatmosferaconpythonypandas-8.png)

Note the annual increase with an upward trend.

The relationship between CO2 and temperature is calculated.

In [45]:

annual_co2_temp = pd.merge(annual_mean_global.loc[1960:2015], annual_co2_ppm.loc[1960:2015], left_index=True, right_index=True)
annual_co2_temp = annual_co2_temp[["LandAndOceanAverageTemperature", "Anomaly", "Carbon Dioxide (ppm)"]].copy()
annual_co2_temp.corr()

Out[45]:

LandAndOceanAverageTemperatureAnomalyCarbon Dioxide (ppm)
LandAndOceanAverageTemperature1.0000001.0000000.923603
Anomaly1.0000001.0000000.923603
Carbon Dioxide (ppm)0.9236030.9236031.000000

In [46]:

sns.heatmap(annual_co2_temp.corr())

Out[46]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fcacea9cdd8>

In [47]:

plt.figure(figsize=(10,8))
sns.scatterplot(x="Anomaly",y="Carbon Dioxide (ppm)", data=annual_co2_temp)

Out[47]:

<matplotlib.axes._subplots.AxesSubplot at 0x7fcace8d6c50>

With the heat map and the scatter plot, the practically linear relationship between the temperature increase and Carbon Dioxide is evident. Both are related to Climate Change. In [ ]:

undefined

The site where the data is stored has more files, for example temperature by city, to go into more detail on the situation per city. You can also keep looking for info on other countries; in the original article Greece was done, and here I tried Argentina, Brazil, Chile, Canada, Mexico and Venezuela.

Make a donation! If you liked the article you can make a donation with Bitcoin (BTC) using the digital wallet of your choice to the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV

Or by scanning the QR code from your wallet:

17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV