0

I have a dataframe with raw data and I would like to select different range of rows for each column, using two different lists: one containing the first row position to select and the other the last.

INPUT

| Index    | Column A | Column B |
|:--------:|:--------:|:--------:|
|      1   |    2     |    8     |
|      2   |    4     |    9     |
|      3   |    1     |    7     |

first_position=[1,2]
last_position=[2,3]

EXPECTED OUTPUT

| Index    | Column A | Column B |
|:--------:|:--------:|:--------:|
|      1   |    2     |    9     |
|      2   |    4     |    7     |

Which function can I use? Thanks!

I tried df.filter but I think it does not accept list as input.

2
  • The question lacks details on the exact logic Commented Dec 1, 2022 at 21:33
  • I´ll have a dataframe with multiple columns, and I have calculated somewhere else the start and end positions of the rows I want to subset per each column. This data has been store in two different lists (start_list, end_list). Probably I can solve this using a for loop... Commented Dec 1, 2022 at 22:09

1 Answer 1

1

Basically, as far as I can see, you have two meaningful columns in your DataFrame.

Thus, I would suggest using "Index" column as the index indeed:

df.set_index(df.columns[0], inplace=True)

That way you might use .loc:

df_out = pd.concat(
    [
        df.loc[first_position, "Column A"].reset_index(drop=True),
        df.loc[last_position, "Column B"].reset_index(drop=True)
    ],
    axis=1
)

However, having indexes stored in separate lists you would need to watch them yourselves, which may be not too convenient.

Instead, I would re-organize it with slicing:

df_out = pd.concat(
    [
        df[["Column A"]][:-1].reset_index(drop=True),
        df[["Column B"]][1:].reset_index(drop=True)
    ],
    axis=1
)

In either cases, index is being destroyed. If that matters, then the scenario without .reset_index(drop=True) would be required.

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.