Home > Net >  Python theory question and dataframe management
Python theory question and dataframe management

Time:03-02

I am very lost, I need to go through a dataframe validating a condition in each row, if the condition is met DO NOT delete the row and if it is met delete it.

In the validation I need to consult previous rows for each row, that is, for each row to see the rest, and not from the same column, I need the entire row.

I can't find any method on the internet to see the entire dataframe from the perspective of the previous row, I can only see that row.

I think iterrows can work, but I don't know how to implement it.

CodePudding user response:

I suppose you need something like this:

import pandas as pd

df = pd.DataFrame({'c1': [10, 11, 12, 55, 61], 'c2': [100, 110, 120, 16, 44]})

for i in range(1,len(df)):
    # if column 0 of previous row "i-1" is even:
    if df.iloc[i-1,0]%2 == 0:
        # print the entire row "i"
        print(df.iloc[i,:])
    else:
        # delete the row "i"
        df = df.drop(labels=i)
  • Related