0

On interating a Boolean comparision array over a dataframe , pandas doesn't consider the boolean array values but rather considers all the values as TRUE and iterates with only the true condition .How to iterate over a pandas dataframe with Boolean Array(vectorisation support)?

OUTPUT :

df=    col datatype
0  1.0    Check
1  1.0    Check
2  0.0    Check
3  NaN    Check
4  1.1    Check

df_bool = 0     True
1    False
2     True
3     True
4     True
Name: col, dtype: bool

df_datatype =    col    datatype
0  1.0  Acceptable
1  1.0  Acceptable
2  0.0  Acceptable
3  NaN  Acceptable
4  1.1  Acceptable

CODE:

   import pandas as pd
from numpy import nan

def vectorisationCompare():
    d = {"col": [1, 1, 0, nan, 1.1], "datatype": ["Check", "Check", "Check", "Check", "Check"]}
    df = pd.DataFrame(data=d)
    print(f'df= {df}')
    df_compare = df.copy()



    df_bool = (df_compare["col"].ne(df_compare["col"].shift(periods = 1)))
    print()
    print(f'df_bool = {df_bool}')

    # Iterating Boolean Array Comparision
    if df_bool.array:
        df["datatype"] = "Acceptable"
    else : df["datatype"] = "UnAcceptable"

    print()
    print (f'df_datatype = {df}')

vectorisationCompare()
srt111
  • 1,079
  • 11
  • 20

0 Answers0