0

I have an array with index numbers

array=[2,3,4,5,234,653.......2500,2501]

I need to copy all rows from DF A with these index numbers(from array) into a new DF

1
  • can't you just loop through the index numbers array, and use the value as the index into the DF array, and the resulting value of the second array you add to a third initially empty DF array? Commented Jun 30, 2017 at 16:21

1 Answer 1

1

Easy- use loc:

# Random array
idx
array([1, 4, 6])

# random dataframe
df
    a
0   1
1   3
2   5
3   7
4   7
5  34
6   3
7  24

new_df = df.loc[idx]
new_df
   a
1  3
4  7
6  3

Passing an array to loc will select rows where index is in the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.