EN VI

Python - Finding if a specific value is in a given column of a dataframe?

2024-03-12 02:30:05
Python - Finding if a specific value is in a given column of a dataframe

I am new to Python and tried to use the following:

pos = pd.DataFrame(columns=['id', 'pred'])
pos.loc[1,'id'] = [4, 4, 4]
pos.loc[2,'id'] = [2, 3, 3]
list1 = [2, 3, 3]
list2 = [3, 2, 3]
print(pos)
print(list1 in pos['id'].values)
print(list2 in pos['id'].values)

It gives me the Error:

ValueError: operands could not be broadcast together with shapes (2,) (3,)

Solution:

You can use any():

list1 = [2, 3, 3]
list2 = [3, 2, 3]

print(any(l == list1 for l in pos["id"]))
print(any(l == list2 for l in pos["id"]))

Prints:

True
False
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login