how to select rows from a dataframe between two lists of index?

Viewed 193

I'm new to python, and I'm working on a very raw dataset.

lets say I have two index list x and y,

x is like 1,10, 20, 25, 37 y is like 6,15, 24, 29, 39

I know how to select rows based on 1 index list, such as:

df.iloc[x]

but I was wondering if there is any way I could select rows between two index list?

something like:

df.iloc[x:y]

so, from above, I can get all the rows between 1:6, 10:15

Thank you so much

P.S after I get all the rows between two list, there will be many dataframes I guess. So how could I loop that?

so far I can think of,

for x, y in zip(list_1, list_2):
pd.merge(df.iloc[x:y])

but it's wrong:(

4 Answers

You could write it like df.iloc[x[i]:y[j]], where i and j can be used as indexes.

Yes you can do that , code will be like df.iloc[x[0]:y[0]]. Index for lists x and y can be variables if you have some logic to run.

TRY:

x = [1, 10, 20, 25, 37]
y = [6, 15, 24, 29, 39]

for i, j in zip(x, y):
    # select row from i to j and then select all columns.
    print(df.iloc[i:j, :])

If instead of printing you wanna concat all df's use:

result = pd.concat([df.iloc[i:j, :] for i, j in zip(x, y)])

NOTE: You can use iloc like shown below, and you can also play with step size if you want:

dataframe.iloc[start_index:end_index,start_col:end_col]

Taking advantage of boolean indexing, the following can be done:

import numpy as np
import pandas as pd

df = pd.DataFrame() # your dataframe here

x = [1, 10, 20, 25, 37]
y = [6, 15, 24, 29, 39]

indices = np.zeros(len(df.index))

for i, j in zip(x, y):
    indices[i:j] = 1

print(df.loc[indices])
Related