JavaBeginner - 9 months ago 62

Python Question

I have an array with numbers which corresponds to the row numbers that need to be selected from a DataFrame.

For example,

`arr = np.array([0,0,1,1])`

`arr`

`Index A B C D`

3 10 0 0 0

4 5 2 0 0

Using

`arr`

`Index A B C D`

3 10 0 0 0

3 10 0 0 0

4 5 2 0 0

4 5 2 0 0

Answer

You can use `iloc`

with integer indexing:

```
df.iloc[[0,0,1,1], :] # or df.iloc[arr, :]
# A B C D
#Index
#3 10 0 0 0
#3 10 0 0 0
#4 5 2 0 0
#4 5 2 0 0
```

Source (Stackoverflow)