我有一个示例数据框df
和一个数组n
,如下所示。我想根据索引中的数组值进行过滤。输出数据帧也如下所示。我尝试了Out = df[df.index == n]
和Out = df.loc[df.index == n ]
,但给出错误Lengths must match to compare
却不起作用。谁能帮我解决这个问题。
df = Date Open High Low Close Adj Close Volume0 2007-06-18 0.33979 0.33979 0.33979 0.33979 0.33979 15918881 2007-06-29 0.33074 0.33074 0.33074 0.33074 0.33074 884402 2007-06-20 0.33526 0.33526 0.33526 0.33526 0.33526 35383 2007-06-21 0.32113 0.32113 0.32113 0.32113 0.32113 35504 2007-06-22 0.34713 0.34713 0.34713 0.34713 0.34713 6706 2007-06-18 0.33979 0.33979 0.33979 0.33979 0.33979 15918887 2007-06-29 0.33074 0.33074 0.33074 0.33074 0.33074 884408 2007-06-20 0.33526 0.33526 0.33526 0.33526 0.33526 35389 2007-06-21 0.32113 0.32113 0.32113 0.32113 0.32113 355010 2007-06-22 0.34713 0.34713 0.34713 0.34713 0.34713 670
数组([0,1,2,3])
出= Date Open High Low Close Adj Close Volume0 2007-06-18 0.33979 0.33979 0.33979 0.33979 0.33979 15918881 2007-06-29 0.33074 0.33074 0.33074 0.33074 0.33074 884402 2007-06-20 0.33526 0.33526 0.33526 0.33526 0.33526 35383 2007-06-21 0.32113 0.32113 0.32113 0.32113 0.32113 3550
最佳答案
你应该能够做
out = df[df.index.isin(n)]
关于python - 通过数组过滤DataFrame索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51368357/