Friday, May 13, 2022

[FIXED] How can I use iloc to extract rows of a dataframe to create a separate dataframe?

Issue

I have a dataframe from which I would like to extract specific rows and create a separate dataframe consisting of those extracted rows.

The following is an example:

df =

Col1 Col2 Col3
0 red blue yellow
1 monago orange apple
2 five six seven

I have the row indices in an array called row_ind with values (0,2):

for i in range(len(df))
    a = row_ind[i]
    b = df.iloc[a]

How can I use this to create the data set that I want?


Solution

foo = []

for i in range(len(df))
    a = row_ind[i]
    foo.append(a)

newdf = df.iloc[foo]


Answered By - Igor Rivin
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.