StanleyWei
StanleyWei

Reputation: 13

Find the index of multi-rows

Suppose I have a dataframe called df as follows: 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] How to list the index [0,3,4] for Apple in A_column?

Upvotes: 1

Views: 37

Answers (1)

gandhi_nn
gandhi_nn

Reputation: 171

You can just pass the row indexes as list to 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

EDIT: seems i misunderstood your questions

So you want to have the list containing the index number of the rows containing 'Apple', you can use df.index[df['A_column']=='Apple'].tolist()

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

Upvotes: 1

Related Questions