How to iterate through Pandas dataframe from specified row number

Viewed 35

I am iterating through a dataframe using the df.iterrows() function but am not sure how to specify the row number to run through the dataframe from. I am using a row counter in the initial for loop (below) to search for X within the rows, and once it has found X, I need to loop through the rest of the dataframe to find Y, but without looping through the initial rows that were already looped through when searching for X.

I have tried to achieve this by deleting all rows up to X, but this does not work as it remove entries I need later after the initial X and Y have been found, and I need to find the next X and Y.

row_count = 0

for index, row in new_df.iterrows():
        if X in row[2]:
            row_count += 1
            # take information required from row
            
            for visit_index, visit_row in new_df.iterrows():
                if Y in visit_row[2]:
                    # take information required from row
                    
                    # append information to new dataframe
                    
                    break
        else:
            new_df.drop(index, inplace = True)  
            row_count += 1

What I want to do instead is use the row_count so that when I find X I can then iterate through the dataframe again from the row where X was present onwards, how can I do this?

1 Answers

You can do this in a much more simple way I believe.

Using the .loc function of pandas you could do something like this:

subset = df.loc[df["YOUR_COLUMN_NAME"].str.contains(X)]

And this would return the subset of rows in your dataframe that contain X in the column "YOUR_COLUMN_NAME". You haven't specified the name of row[2] but use that instead of "YOUR_COLUMN_NAME"

As an example, my code:

import pandas as pd

df = pd.DataFrame([[1, "Test1.1"], [2, "Test2.1"]], columns=["ID", "STR"])


x = df.loc[df["STR"].str.contains("Test1")]
print(x)

Outputs this:

   ID      STR
0   1  Test1.1

From here you could take whatever information you needed from the row.

Related