Skip to content

Instantly share code, notes, and snippets.

@himat
Last active July 2, 2019 00:40
Show Gist options
  • Save himat/99c6647829998362b548a80644a08905 to your computer and use it in GitHub Desktop.
Save himat/99c6647829998362b548a80644a08905 to your computer and use it in GitHub Desktop.
[Check if value in df col] You have to check if `val in col.values`, not just `val in col` #pandas #dangerous
# Doing `x in df["col"]` will always look for x in the *index* of the df, not the actual column values
# Thus, you have to use `x in df["col"].values` instead
# This can be especially insidious when you're checking if a number is in a col, but that number is also in the index.
# If you don't use `.values` in this case, you will think the value is in the col when it actually isn't!
>>> df = pd.DataFrame({"a": [20, 40, 1], "b": [32, 42, 52]})
>>> df
a b
0 20 32
1 40 42
2 1 52
>>> 1 in df["a"]
True
>>> 20 in df["a"]
False
>>> 1 in df["a"].values
True
>>> 20 in df["a"].values
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment