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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With