I have been trying to test if any value in a series appears in a column in a dataframe, however it appears that the way I have been doing it returns True regardless of whether the values appear or not.
For example, using the below dataframe and series:
df = pd.DataFrame({'Col1' : [1, 2, 2, 6, 4, 8],
'Col2' : [11, 11, 11, 11, 11, 11]})
Col1 Col2
0 1 3
1 2 3
2 2 3
3 6 3
4 4 3
5 8 3
series = pd.Series([3, 5, 9])
0 3
1 5
2 9
dtype: int64
I want to check if any value in series appears in Col1 (which it doesn't in this example, so should evaluate to False). The method I've been using to check this is:
if True in series.isin(df['Col1']):
Which evaluates to True despite the fact that the output of series.isin(df['Col1'])
is a series of only False
. I was wondering why this is the case and is there a better way to check if a value in a series is in a dataframe? I know I can use if any(series.isin(df['Col1']))
, is this the best way?
(Pandas version: 1.1.3)
CodePudding user response:
aggregate using any
:
if series.isin(df['Col1']).any():
# do something
Why your approach failed
True in series.isin(df['Col1'])
check if there is True
in the index of the series, not its values. This would work with True in series.isin(df['Col1']).values
Although it doesn't return True
for me, I suspect in your case True in series.isin(df['Col1'])
might work as you have 1
in the indices and True
is equivalent to 1
(which pandas version do you have?).
CodePudding user response:
You can check overlap with
overlap = series[series.isin(df['Col1'])]
If you are only interested in checking for emptiness - or no overlap - then you can use the empty
property
series[series.isin(df['Col1'])].empty
# True