部分表:
代码
rows = idx['中山':'珠海','2019-07-02':'2019-07-04']
cols = idx['白天',['最高温度','天气状况']]
df.loc[rows,cols]
问题:
UnsortedIndexError: 'MultiIndex slicing requires the index to be lexsorted: slicing on levels [0, 1], lexsort depth 0'
解决方法:
因为pandas 需要根据位置范围确定切片范围中的数据,由于索引没有排序,这样乱取 数据会出错
df.index.is_monotonic_increasing #显示索引是否已排序
>>>False #表明未排序
df = df.sort_index() #对df的索引进行排序
重新运行语句:
rows = idx['中山':'珠海','2019-07-02':'2019-07-04']
cols = idx['白天',['最高温度','天气状况']]
df.loc[rows,cols]
结果: