I have this toy example which capture my real problem:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['car there is','car not working', 'bus there is']})
df.iloc[1] = np.nan
idx = df['A'].str.contains('car')
df['IsCar'] = 0
df.loc[idx,'IsCar'] = 1
When I try to run this code, I got the following error message:
ValueError: cannot index with vector containing NA / NaN values
Why can I not do this. Is there fix where I not have to replace the NaN with something else?
There is a flag na
for str.contains
(see docs) which you can set to False, which will provide a fill value for missing values. Simply use
idx = df['A'].str.contains('car', na=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With