If I want to loop through values in a Series, I can do that using the in
operator
[x for x in pd.Series(['Hello, World!'])]
> ['Hello, World!']
but if I use in
to check if Hello, World!
is in the Series, it returns False
.
'Hello, World!' in pd.Series(['Hello, World!'])
> False
Paradoxically (to the untrained eye), this behavior makes the following list comprehension return empty:
hello_series = pd.Series(['Hello, World!'])
[x for x in hello_series if x in hello_series]
> []
This is Series-specific behavior; it of course works fine with lists:
'Hello, World!' in ['Hello, World!']
> True
Why does in
work in one context and not the other with Series, and for what reason(s)?