Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas not finding elements in Columns

Tags:

python

pandas

Pandas does not seem to find all elements in a list:

df = pd.DataFrame({"rid": ["125264429", "a"], "id": [1, 2]})
1 in df["id"]                # <- expect True, get True
"125264429" in df["rid"]     # <- expect True, get False
df[df["rid"] == "125264429"] # <- yields result

I am sure there is a perfectly reasonable explanation for this behaviour, but I can't seem to find it. It seems that the last two columns contradict each other. Does it have to do something with the fact that the datatype of the "rid" column is object?

like image 931
user1965813 Avatar asked Jun 26 '26 02:06

user1965813


1 Answers

If use in operator it test not values of Series/column, but index values, docs:

print(1 in df["id"])              # <- expect True, get True
print("125264429" in df["rid"])     # <- expect True, get False 

is same like:

print(1 in df["id"].index)              # <- expect True, get True
print("125264429" in df["rid"].index)     # <- expect True, get False

So if convert values to numpy array or list it working like expected:

print(1 in df["id"].values)              # <- expect True, get True
print("125264429" in df["rid"].values)     # <- expect True, get True

print(1 in df["id"].tolist())              # <- expect True, get True
print("125264429" in df["rid"].tolist())     # <- expect True, get True
like image 91
jezrael Avatar answered Jun 27 '26 16:06

jezrael