vendredi 22 mai 2020

If statements in dataframe over multiple rows

I have a column in a DataFrame which has zero values and non zero values Extras. I need to classify the non zero values into 2 categories True and False in a 4th column df['Fair']. To form if statements to separate these I am reliant on values in the row below from different columns as well as whether the Extras values are even or odd but I don't know how to do this. An example of the DataFrame is provided below:

  Bat  Over  Extras
0   A     1       1
1   B     1       0
2   B     1       1
3   B     1       0
4   C     1       1
5   A     2       0

The if statement below needs to classify using the following conditions and maybe could look something like this:

if next row over != this row overs:
  df['Fair']=1
elif (df['Extras'] % 2) == 0 & next row batsman = this row batsman:
  df['Fair']=0
elif (df['Extras'] % 2) != 0 & next row batsman = this row batsman:
  df['Fair']=1

expected output is

  Bat  Over  Extras  Fair
0   A     1       1     1
1   B     1       0     1
2   B     1       1     0
3   B     1       0     1
4   C     1       1     1
5   A     2       0     1

Aucun commentaire:

Enregistrer un commentaire