Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: cannot index with vector containing NA / NaN values

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) 
like image 849
Michael Norman Avatar asked Jun 27 '18 17:06

Michael Norman


2 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) 
like image 68
MEdwin Avatar answered Sep 20 '22 09:09

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.

like image 45
kristina Avatar answered Sep 18 '22 09:09

kristina