pandas string contains lookup: NaN leads to Value Error -
if filter rows string in column value, possible use data.sample_id.str.contains('hph') (answered before: check if string in pandas dataframe column in list, or check if string in pandas dataframe).
however, lookup column contains emtpy cells. terefore, str.contains() yields nan values , value error upon indexing.
`valueerror: cannot index vector containing na / nan values`` what works:
# runs mask = [index index, item in enumerate(data.sample_id.values) if 'zent' in str(item)] is there more elegant , faster method (similar str.contains()) 1 ?
you can set parameter na in str.contains false:
print (df.a.str.contains('hph', na=false)) using edchum sample:
df = pd.dataframe({'a':['hph', np.nan, 'sadhphsad', 'hello']}) print (df) 0 hph 1 nan 2 sadhphsad 3 hello print (df.a.str.contains('hph', na=false)) 0 true 1 false 2 true 3 false name: a, dtype: bool
Comments
Post a Comment