【问题标题】:Find the index of multi-rows查找多行的索引
【发布时间】:2018-10-26 04:20:21
【问题描述】:

假设我有一个名为 df 的数据框,如下所示: A_column B_column C_column 0 Apple 100 15 1 Banana 80 20 2 Orange 110 10 3 Apple 150 16 4 Apple 90 13

[Q]如何在A_column中列出Apple的索引[0,3,4]?

【问题讨论】:

  • 我不确定您的意思是“列出”索引。您的意思是从表中提取第 0、3 和 4 行吗?或者是其他东西?您对此有何期望?
  • 我想从表中获取值 0、3 和 4。

标签: python dataframe indexing row


【解决方案1】:

您可以将行索引作为列表传递给df.iloc

>>> df
  A_column  B_column  C_column
0    Apple       100        15
1   Banana        80        20
2   Orange       110        10
3    Apple       150        16
4    Apple        90        13

>>> df.iloc[[0,3,4]]
  A_column  B_column  C_column
0    Apple       100        15
3    Apple       150        16
4    Apple        90        13

编辑:似乎我误解了你的问题

所以你想让列表包含包含'Apple'的行的索引号,你可以使用df.index[df['A_column']=='Apple'].tolist()

>>> df.index[df['A_column']=='Apple'].tolist()
[0, 3, 4]

【讨论】:

  • 非常感谢,我得到了想要的答案。
猜你喜欢
  • 2018-11-30
  • 2015-03-16
  • 2013-01-07
  • 2022-11-22
  • 2016-12-05
  • 2013-09-08
相关资源
最近更新 更多