
?? 作者:韓信子@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 = https://www.cnblogs.com/showmeai/p/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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/502926.html
標籤:其他
