[Python]Pandas对于非唯一的label index的数据选择问题

结论: 在pandas中,非unique的index无法通过df.loc[index]来达到多行选择的目的。

以下面的数据为例:

month count
1 1
1 2
2 3
2

4

假如我们之前对dataframe做了dataframe.set_index('month', inplace=True)操作,将month转换为index。

则无法通过dataframe.loc[1, 'count']来获取month == 1的子数据, 代码可能会报错keyError或者 xxx is not unique。

即:对于含有非unique的index的dataframe,无法通过label name去定位多行数据。

posted @ 2019-06-08 20:20  oDoraemon  阅读(1220)  评论(0编辑  收藏  举报