1

I'm attempting to create a statement where if an object in my dataframe is notna and has a Book_Status of True, then continue on to the next task. However, when I attempt to do this I get "float" object has no attribute "notna".

I've looked into np.where, but that seems to be used to create a column? For loop using np.where

Example dataframe

name       quote id       Book_Status
Park       foobar300      False
Bus        NaN            False
Car        NaN            False

And here is what my code is that's giving me my error

def BookEvent(df):
    y = 0
    for i in range(len(df_parking.index)):
        if df['quote id'][y].notna() & df['Book_Status'][y] == False:
          # Then do something unrelated to this df
SpindriftSeltzer
  • 322
  • 3
  • 12

1 Answers1

2

In your solution working with scalar, so need pd.notna and instead & use and, but this loop solution is slow:

if pd.notna(df['quote id'][y]) and df['Book_Status'][y] == False:

But in pandas is better/ faster working with masks like:

mask = df['quote id'].notna() & ~df['Book_Status']
df['new'] = np.where(mask, 10, 20)

print (df)
   name   quote id  Book_Status  new
0  Park  foobar300        False   10
1   Bus        NaN        False   20
2   Car        NaN        False   20
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252