结论: 在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去定位多行数据。

转载于:https://www.cnblogs.com/oDoraemon/p/10991571.html

更多推荐