ValueError: cannot index with vector containing NA / NaN values

Question:

I do not understand why I am receiving the error listed in the title, the value that I am intending to return is the number 30

import csv
import os
import pandas as pd
os.chdir('C:\Users\khalha\Desktop\RealExcel')
filename = 'sales.csv'

Sales = pd.read_csv('sales.csv')
iFlowStatus = Sales[Sales['Product'].str.contains('iFlow')]['Status']
print(iFlowStatus)
Asked By: Michael Norman

||

Answers:

The error message means that the dataframe contains blank entries that default to na/NaN.

You can just add na=False in the synatx to fill value for missing values.

import csv
import os
import pandas as pd
os.chdir('C:\Users\khalha\Desktop\RealExcel')
filename = 'sales.csv'

Sales = pd.read_csv('sales.csv')
iFlowStatus = Sales[Sales['Product'].str.contains('iFlow', na=False)]['Status']
print(iFlowStatus)
Answered By: MEdwin

One other possible issue: you can get this with mixed-type columns that do not contain NaN. For example:

> df = pd.DataFrame({'x': ['hi', 99]})
> df.x.isna().any()
False
> df[df.x.str.contains('hi')]
...
ValueError: cannot index with vector containing NA / NaN values

Depending on what you want to do, you can cast (df.x.astype(str).str.contains('hi')) or drop the offending rows.

Answered By: kristina
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.