Climate change
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Analyzing Climate Change from '50s to '00s

    # Import modules
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    # Read the data
    data = pd.read_csv("climate_change.csv", parse_dates=["date"])
    data.head()
    # Quick look at numeric variables
    data.describe()
    # Check for NaN
    data.isnull().sum()
    # Look at NaN
    data[data.co2.isnull()]
    # Drop NaN
    data.dropna(inplace=True)
    data.isnull().sum()
    # Parameters for visualization with Seaborn
    sns.set(rc={"figure.figsize":(15, 7)})
    sns.set_context("notebook")
    sns.set_style("whitegrid")
    # Plot Co2 over time
    sns.lineplot(x="date", y="co2", data=data)
    # Plot relative_temp over time
    sns.lineplot(x="date", y="relative_temp", data=data, color="darkred")
    # Overlap the two plots
    sns.lineplot(x="date", y="co2", data=data)
    ax = plt.twinx()
    sns.lineplot(x="date", y="relative_temp", data=data, color="darkred", ax=ax)
    plt.grid(b=None)
    # Create a new column, decade
    data["decade"] = (np.floor(data.date.dt.year / 10) * 10).astype(int)
    
    # Group by decade and compute the mean for co2 and relative_temp
    grouped = data.groupby("decade", as_index=False)[["relative_temp", "co2"]].mean()
    
    # Plot the results
    ax = sns.barplot(x="decade", y="relative_temp", data=grouped, color="darkred")
    ax1 = ax.twinx()
    ax1.plot(grouped.co2, marker="o")
    ax1.set_ylabel("Co2")
    plt.legend(["Co2"], loc='upper left')
    plt.grid(b=None)
    # Plot relative_temp over co2
    sns.scatterplot(x="co2", y="relative_temp", data=data, hue="relative_temp", palette="icefire", legend = False)
    # Create a new column, after_70s
    data["after_70s"] = ["Yes" if x > 1970 else "No" for x in data.decade]
    
    # Calculate the mean co2 until 1980 (from 1950 to 1979) and after '70s
    mean_co2_until_80 = data.query('after_70s == "No"').co2.mean()
    mean_co2_after_70s = data.query('after_70s == "Yes"').co2.mean()
    
    # Plot relative_temp over co2 with hue = after_70s
    sns.scatterplot(x="co2", y="relative_temp", data=data, hue="after_70s", palette=["green", "darkred"])
    
    # Draw a vertical line for mean co2 until '80s
    plt.axvline(mean_co2_until_80, 0,data.relative_temp.max(),color="black",alpha=0.70)
    plt.text(mean_co2_until_80,1,'Mean Co2 before 1980')
    
    # Draw a vertical line for mean co2 after '70s
    plt.axvline(mean_co2_after_70s, 0,data.relative_temp.max(),color="black", alpha=0.70)
    plt.text(mean_co2_after_70s,1,'Mean Co2 from 1980')