加入飛行時長類別
既然你已經設定好想要標記的類別與值,接下來就來建立一個新的欄位,用來分析各種飛行時長的班次頻率吧!
你先前建立的變數 flight_categories、short_flights、medium_flights 與 long_flights 已可供使用。
另外,以下套件也已匯入:pandas 作為 pd、numpy 作為 np、seaborn 作為 sns,以及 matplotlib.pyplot 作為 plt。
本練習屬於課程
Python 的探索性資料分析
練習說明
- 建立
conditions,其為一個清單,內容為依據short_flights、medium_flights、long_flights對planes["Duration"]進行的子集合。 - 透過呼叫一個能接受你的
conditions清單與flight_categories的函式,建立"Duration_Category"欄位,並將未匹配到的值設定為"Extreme duration"。 - 建立一個圖表,顯示各類別的筆數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create conditions for values in flight_categories to be created
conditions = [
(planes["____"].____.____(____)),
(planes["____"].____.____(____)),
(planes["____"].____.____(____))
]
# Apply the conditions list to the flight_categories
planes["Duration_Category"] = ____.____(____,
____,
default="____")
# Plot the counts of each category
sns.____(data=____, x="____")
plt.show()