我有一个示例数据帧df
和一个数组n
,如下所示。我想根据索引中的数组值进行过滤。输出数据帧也如下所示。我尝试了Out = df[df.index == n]
,Out = df.loc[df.index == n]
和df.loc[n]
,但均无法正常工作,并给出错误Lengths must match to compare
。谁能帮我解决这个问题。这里的数组是对应于数据帧的行号。
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
答案 0 :(得分:3)
用于切片的熊猫符号:
df.iloc[0:4,:]
答案 1 :(得分:3)
使用DataFrame.iloc
用于按位置选择:
.subscribe((Consumer<? super List<String>>)getAnimalsObserver());
答案 2 :(得分:1)
用输入内容替换<>之间的所有内容
# slice by column position
df.iloc[<start_row>:<end_row>, <column_start_position>:<column_end_position>]
# for everything in a column
df.iloc[:, <column_position>]
# slice by column name
df.loc[<start_row>:<end_row>, <column_name>]
# for everything in a column
df.loc[:, <column_name>]
也请在pandas文档中查看Index and Selecting Data。超级丰富,即使在第一次通过时也不会造成混淆。