0

考虑玩具数据帧 df1 和 df2,其中 df2 是 df1 的子集(不包括第一行)。

将 pandas 导入为 pd 将 numpy 导入为 np

df1 = pd.DataFrame({'colA':[3.0,9,45,7],'colB':['A','B','C','D']})
df2 = df1[1:]

现在让我们为每一帧找到 colA 的 argmax

np.argmax(df1.colA) ## result is "2", which is what I expected
np.argmax(df2.colA) ## result is still "2", which is not what I expected.  I expected "1" 

如果我的兴趣矩阵是 df2,我该如何解决这个索引问题?这个怪癖是与 pandas、numpy 还是只是 python 内存有关?

4

2 回答 2

1

我认为这是由于索引。您可以reset_index在分配时使用df2

df1 = pd.DataFrame({'colA':[3.0,9,45,7],'colB':['A','B','C','D']})
df2 = df1[1:].reset_index(drop=True)

In [464]: np.argmax(df1.colA)
Out[464]: 2

In [465]: np.argmax(df2.colA)
Out[465]: 1

我认为最好使用方法argmax而不是np.argmax

In [467]: df2.colA.argmax()
Out[467]: 1
于 2015-12-11T19:05:29.277 回答
0

您需要重置的索引df2

df2.reset_index(inplace=True, drop=True)
np.argmax(df2.colA)
>> 1
于 2015-12-11T19:05:41.790 回答