按索引值排序
之前,您通过调用 .sort_values() 更改了 DataFrame 的行顺序。按索引中的元素排序同样很有用。要实现这一点,您需要使用 .sort_index()。
pandas 已以 pd 导入。temperatures_ind 具有 country 和 city 的多级索引,且已提供。
本练习是课程的一部分
使用 pandas 进行数据处理
练习说明
- 按索引值对
temperatures_ind进行排序。 - 按
"city"层级的索引值对temperatures_ind进行排序。 - 先按国家升序、再按城市降序对
temperatures_ind进行排序。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Sort temperatures_ind by index values
print(____)
# Sort temperatures_ind by index values at the city level
print(____)
# Sort temperatures_ind by country then descending city
print(____)