布林索引與 Matplotlib 趣味練習
現在來看看布林索引如何讓你只用幾行程式碼就能用視覺化探索資料。這個練習會綜合你學到的重點——把字典轉成可用的 pandas DataFrame、用布林值進行索引,然後用 matplotlib 視覺化資料,從野生動物撞擊(wildlife strikes)資料中找出一些關係。
本練習屬於課程
給 MATLAB 使用者的 Python
練習說明
- 將
strikes字典轉為 DataFrame。 - 在
Engine欄位中為'Turbofan'建立布林篩選器。 - 在
Engine欄位中為'Turboprop'建立布林篩選器。 - 使用
turbofan與turboprop作為篩選條件,對strikes資料集繪製兩張散佈圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a dictionary and then a DataFrame from the dictionary
strikes = {'Date': date,'Speed': speed,'Height':height,'Engine':engine}
strikes = pd.____(strikes)
# Filter strikes by engine type
turbofan = strikes['Engine']=='____'
turboprop = strikes['____']=='____'
# Create scatter plot of speed and height for each engine type
plt.scatter(strikes[____]['Speed'],strikes[____]['Height'],label='Turbofan')
plt.scatter(strikes[____]['Speed'],strikes[____]['Height'],label='Turboprop')
plt.legend()
plt.xlabel('Strike speed (knots)')
plt.ylabel('Strike height (feet)')
plt.show()