自定义行和列选择熊猫

时间:2021-03-29 07:33:01

标签: pandas

我希望从 Pandas 中选择自定义行和自定义列。

我找到了所需行的索引,它们是。

indices_rows = [224, 290, 390, 130, 262, 338]

以及我希望在我的 DataFrame 中拥有的第一列的索引和最后一列的索引。

index_first = 26index_last = 56229

当我想用

提取所需的DataDrame时
new_df = data_raw.iloc[[indices_rows],index:index_last] 

出现错误:ValueError: Buffer has the wrong number of dimensions (expected 1, got 2)

我不知道为什么。因为它们分开工作。

1 个答案:

答案 0 :(得分:1)

移除[]周围的indices_rows

new_df = data_raw.iloc[indices_rows,index:index_last]

因为:

new_df = data_raw.iloc[[indices_rows],index:index_last] 

是:

new_df = data_raw.iloc[[[224, 290, 390, 130, 262, 338]],index:index_last]

并且需要:

new_df = data_raw.iloc[[224, 290, 390, 130, 262, 338],index:index_last]