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
  • 75
  • 10

2 Answers2

0

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
Vlad Siv
  • 2,346
  • 1
  • 9
  • 19
0
>>> (pd.Series([24.02, 149, 123.11]) == 149.0).any()
True
Steele Farnsworth
  • 814
  • 1
  • 5
  • 12