I have a csv that I'm loading into a dataframe. I only need the rows for which the Organization
column contains a target string affiliation
.
When I try to use str.contains()
I get ValueError: cannot index with vector containing NA / NaN values
.
I've looked at Value Error when Slicing in Pandas and pandas + dataframe - select by partial string and the following solution that both have works for me:
df = df[df['Organization'].str.contains(affiliation)==True]
or
df = df[df['Organization'].str.contains(affiliation).fillna(False)]
But, as a test, I did this:
print(len(df)) #99228
df = df[pd.notnull(df['Organization'])] #or df = df.dropna(subset=['Organization'])
print(len(df)) #99228
df = df[df['Organization'].str.contains(affiliation).fillna(False)]
print(len(df)) #1605
My question is: the ValueError
I was getting without ==True
or fillna(False)
attached to str.contains()
seems to imply that the Organization
column has NaNs
. But then why do I get the same sized df after keeping only the non-null Organization
rows? What am I missing here?
Thanks!