【发布时间】:2016-07-07 17:15:01
【问题描述】:
我在result 变量中有一个pandas DataFrame 并想从中提取一些行,具体而言,将它们按ids 对分组并选择对应于最小值scores 的行。
代码如下:
gb = result.groupby(['id1', 'id2'], sort=False, group_keys=False, as_index=False)
result1 = result.loc[gb['score'].idxmin()].dropna()
我已停止调试器中的代码以检查结果的正确性。结果很奇怪。
>>> gb['score'].idxmin().shape
Out[11]: (1800L,)
>>> result1.shape
Out[12]: (1810, 6)
这 10 行是从哪里来的?!
此外,我在 Jupyter notebook 中以交互方式在同一个数据文件上运行完全相同的代码,并且有 1800 行。
我正在使用 Anaconda 进行所有更新。这是版本字符串
Python 2.7.11 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)]
【问题讨论】:
-
确实,问题出在非唯一索引上。数据框
result来自pd.concat([df1, df2])。添加ingore_index=True解决了这个问题。 Jupyter 单元确实包含ignore_index=True。
标签: python pandas dataframe anaconda