Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: bad operand type for unary ~: float

Tags:

python

pandas

df = df[~df["column"].str.contains("Total")]  TypeError: bad operand type for unary ~: 'float' 

Why does .str.contains() return a float? What should I be doing here?

like image 572
fredley Avatar asked Sep 12 '18 14:09

fredley


1 Answers

I think there are NaNs values, so need specify parameter na:

df = pd.DataFrame({     'column': ['Total','a',np.nan],     'B': list(range(3)) }) print (df)   column  B 0  Total  0 1      a  1 2    NaN  2  df = df[~df["column"].str.contains("Total", na=False)] print (df)   column  B 1      a  1 2    NaN  2 
like image 85
jezrael Avatar answered Sep 20 '22 15:09

jezrael