布尔索引与 Matplotlib 趣味实践
现在来看看,布尔索引如何帮助我们用几行代码就能进行可视化探索。在本练习中,您将综合运用已学内容:把字典转换为可用的 pandas DataFrame,使用布尔索引进行筛选,然后用 matplotlib 可视化数据,从野生动物撞击数据中发现一些关系。
本练习是课程的一部分
面向 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()