Home > other >  Iterate through a dataset in Python (Pandas) to only print out the false values:
Iterate through a dataset in Python (Pandas) to only print out the false values:

Time:09-21

I am trying to iterate the df so that only the values which are false are printed out. In order to then eventually send emails to all the False values (i.e. they have not taken annual leave). Need help with the syntax for the loop.

CodePudding user response:

You can use ~ operator to negate a condition, so:

df[~df.EmployeeLeaveTaken]

will print out the rows where EmployeeLeaveTaken is False.

CodePudding user response:

To filter on the True values you can do something like this:

only_true= df.loc[df['EmployeeLeaveTaken']==True]

In case you want to do something on each record instead you could also just do a for loop as you said and one by one perform an action if the employee has taken a leave:

for i in range(len(df)):
    if df.loc[i, "EmployeeLeaveTaken"] == True:
        print(df.loc[i])
        # Do something

As you can see in both instances loc[...] allows you to select the records based on index.

  • Related