2

I have one data frame and I would like to create a second dataframe using only select index values from the first data frame.

for example:

If I wanted dataframe 2 to be only index values- (47,55,69) from dataframe 1 I would like all of the data within the row from each index value to be transferred

2
  • 2
    df2 = df.iloc[[47,55,69]] ? Commented May 2, 2020 at 20:31
  • In addition, read the documentation carefully. Commented May 2, 2020 at 22:23

1 Answer 1

3

There are several different options for slicing dfs. Using .iloc is one of the simpler ways for integer index selections and it has good performance. You can also use the .isin() method. It is generally a little slower than .iloc, although it offers additional flexibility for more complicated selection scenarios.

slice_list = [47,55,69]

df_2 = df_1.iloc[df_1.index.isin(slice_list)]
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

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.