I have a sample dataframe df
and an array n
as shown below. I want to filter based on the array values which are in index. The output dataframe is shown below as well. I have tried Out = df[df.index == n]
, Out = df.loc[df.index == n]
and df.loc[n]
which is not working giving an error Lengths must match to compare
. Can anyone help me in solving this. Here the array is the row number corresponding to data frame.
df =
Open High Low Close Adj Close Volume
2007-06-18 0.33979 0.33979 0.33979 0.33979 0.33979 1591888
2007-06-29 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2007-06-20 0.33526 0.33526 0.33526 0.33526 0.33526 3538
2007-06-21 0.32113 0.32113 0.32113 0.32113 0.32113 3550
2007-06-22 0.34713 0.34713 0.34713 0.34713 0.34713 670
2007-06-16 0.33979 0.33979 0.33979 0.33979 0.33979 1591888
2007-06-30 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2007-06-31 0.33526 0.33526 0.33526 0.33526 0.33526 3538
2007-06-44 0.32113 0.32113 0.32113 0.32113 0.32113 3550
2007-06-22 0.34713 0.34713 0.34713 0.34713 0.34713 670
n = array([0, 1, 2, 3])
Out =
Open High Low Close Adj Close Volume
2007-06-18 0.33979 0.33979 0.33979 0.33979 0.33979 1591888
2007-06-29 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2007-06-20 0.33526 0.33526 0.33526 0.33526 0.33526 3538
2007-06-21 0.32113 0.32113 0.32113 0.32113 0.32113 3550