氣候是全球性的話題,本文基于owid co2資料集,分析了世界各地的二氧化碳排放量,并将二氧化碳排放的主要國家以及二氧化碳排放來源進行了可視化。
💡 作者:韓信子@ShowMeAI
📘 資料分析實戰系列:https://www.showmeai.tech/tutorials/40
📘 本文位址:https://www.showmeai.tech/article-detail/322
📢 聲明:版權所有,轉載請聯系平台與作者并注明出處
📢 收藏ShowMeAI檢視更多精彩内容
ShowMeAI在本篇内容中整理了一個資料科學學習的基本項目,我們會分析世界各地的二氧化碳排放量,我們可以看到二氧化碳排放的主要國家以及導緻二氧化碳排放的不同來源。這也是『碳中和』大環境下大家關心的主題之一。
大家可以使用本地的jupyter notebook來運作我們下述代碼,也可以使用 Google Colab 或 Kaggle notebook來運作。本項目使用的 🏆owid co2 data資料集,大家可以通過ShowMeAI的百度網盤下載下傳擷取。
🏆 實戰資料集下載下傳(百度網盤):公衆号『ShowMeAI研究中心』回複『實戰』,或者點選 這裡 擷取本文 [23]碳中和背景下的二氧化碳排放資料分析 『owid co2 data資料集』
⭐ ShowMeAI官方GitHub:https://github.com/ShowMeAI-Hub
💡 資料處理
資料分析處理涉及的工具和技能,歡迎大家查閱ShowMeAI對應的教程和工具速查表,快學快用。
- 圖解資料分析:從入門到精通系列教程
- 資料科學工具庫速查表 | Pandas 速查表
- 資料科學工具庫速查表 | Seaborn 速查表
首先,我們将導入庫:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
讀取資料:
dataset = pd.read_csv('owid-co2-data.csv')
檢視資料:
有兩個核心的函數可以幫助我們檢視資料基本形态:
dataset.head() :顯示資料集的前5行。 如果您想檢視更多行,調整括号中數字即可。例如dataset.head(10)檢視前10行
dataset.shape :顯示資料集的行數和列數
我們本次分析的資料集有 25204 行和 58 列。
删除列:
我們可以做一些資料處理,比如删除一些資料分析中不适用的字段/列。
df = dataset.drop(columns=[ 'consumption co2','co2 growth _prect','co2 growth_abs' ])
df.head()
熟悉pandas的同學也知道,我們也可以直接通過字段名選擇需要分析的字段,如下代碼所示:
df1 = df[['country', 'year','co2','coal_co2','cement _co2', 'flaring_co2','gas_co2','oil _co2', 'other industry co2','methane', 'nitrous_oxide', 'population' ]]
df1
我們可以通過pandas的條件選擇來選取資料子集:
final df = df1[df1['year' ]>1995]
final df
可以通過isin等函數來框定類别型字段的取值,例如下述代碼:
final_df = final_df[(final_df['country'].isin(['United States', 'Africa', 'Antartica','South Korea', 'Bangladesh', 'Canada', 'Germany', 'Brazil', 'Argentina','Japan', 'India', 'United Kingdom', 'Saudi Arabia', 'China', 'Australia','Russia']) & (final_df['co2'] > 0))]
final_df
檢查缺失值:
final_df.isnull().sum()
💡 資料分析&可視化
我們将根據我們的資料集繪制圖表并分析一些結果。 我們繪制一下随時間線的co2排放趨勢圖:
px.line(dataset, x = 'year', y = 'co2', color='country')
我們按照co2排放量為大小繪制散點圖,如下
px.scatter(dataset[dataset['year']==2019], x="co2_per_capita", y="energy_per_capita", size="co2", color="country", hover_name="country", log_x=True, size_max=60)
選出各大洲的資料
continent_data = dataset[(dataset['country'].isin(['Europe', 'Africa', 'North America', 'South America', 'Oceania', 'Asia'])) & (dataset['co2'] > 0)]
continent_data
從各大洲來看的占比情況如下
px.pie(continent_data, names='country', values='co2')
下面,我們将根據國家和 Co2 列繪制餅圖,看看哪些國家的 Co2 排放量最高。
px.pie(final_df, names='country', values='co2')
我們可以看到中國、美國、印度都是主要的co2排放大國。如果我們根據二氧化碳的來源進一步分析并僅看 2020 年,那麼我們将得到以下結果:
final df 2020 = final _df[(final_df[ 'year' ]==2020) ]
final df 2020
final df 2020[['country','coal_co2','cement_co2','flaring _co2','gas_co2', 'oil _co2','other_ industry co2']].plot(x='country', kind='bar',figsize=(9,5),width=0.9)
plt.title(‘'2020 CO2 consumption')
plt.xlabel('Countries' )
plt.ylabel('CO2 measured in million tonnes')
上面的結果中,我們可以看到,三個主要的 Co2 來源是 coal_co2、oil_co2 和 gas_co2。 我們針對這三個主要來源做一點繪圖分析,結果會更清晰:
final_df_2020[(['country','coal_co2','gas_co2','o0il_co2')].plot(x='country', kind='bar' ,figsize=(9,5),width=0.9)
plt.title('2020 CO2 consumption')
plt.xlabel('Countries')
plt.ylabel('CO2 measured in million tonnes')
如果我們標明美國進行進一步分析:
final_df_US = final_df[(final_df['country']=='United States']
final_df_US[['year','coal_co2','gas_co2','0il_co2']].plot(x='year', kind='bar',figsize=(10,6),width=0.9)
plt.title('1996 onwards CO2 emission in United States')
plt.xlabel('Year' )
plt.ylabel(‘'CO2 measured in million tonnes')
圖例顯示,在美國,coal_co2 和 oil_co2 随着時間的推移而減少,但 gas_co2 多年來一直在增加。
💡 總結
全球氣候是全世界都很關心的主題,本篇内容是 Co2 排放的一些簡單分析和可視化,大家可以基于上述資料與字段做進一步分析。全球變暖是一個大問題,每個國家都在共同努力,營造更好的環境。
參考資料
- 📘 圖解資料分析:從入門到精通系列教程:https://www.showmeai.tech/tutorials/33
- 📘 資料科學工具庫速查表 | Pandas 速查表:https://www.showmeai.tech/article-detail/101
- 📘 資料科學工具庫速查表 | Seaborn 速查表:https://www.showmeai.tech/article-detail/105