0

I need to check if a float is in a dataframe column. See this code:

import pandas as pd

list1 = [24.02, 149, 123.11]
imp = 149.0

df = pd.DataFrame()
df['List1'] = list1

If I run:

imp in df['List1']
>> False

I expected to get True in return ... How shall I improve my code?

Andrew
  • 89
  • 1
  • 12

2 Answers2

1

in of a Series checks whether the value is in the index, if you want to check if value is in values use x in df['List1'].values

import pandas as pd

list1 = [24.02, 149, 123.11]
imp = 149.0

df = pd.DataFrame()
df["List1"] = list1

print(1 in df["List1"])
>>> True
print(imp in df["List1"].values)
>>> True
vladsiv
  • 2,718
  • 1
  • 11
  • 21
1
>>> (pd.Series([24.02, 149, 123.11]) == 149.0).any()
True
Steele Farnsworth
  • 863
  • 1
  • 6
  • 15