Pandas check if dataframe column contains value from list (different lengths)

Viewed 344

I have the following DataFrame df1:

  A
0 E2
1 27
2 99
3 NaN
4 20
5 14

And the following list:

list1 = [14, 61, 27, 82, 79, 75, 44, 10, 'E2','E9']

I want to add/append a new column called 'B' to df1 that checks whether the values in column 'A' are in list1 by returning a boolean.

Ultimately, I would want the following output in df1:

  A   B
0 E2  True
1 27  True
2 99  False
3 NaN False
4 20  False
5 14  True

Most of the questions I have seen regarding this have been when the length of the DataFrame column has been the same as the length of the list.

Does anyone know how to do this when the lengths are different like in this case?

Thanks a lot!

2 Answers

Use Series.isin, only necessary match numeric with numbers types for correct working:

df['B'] = df['A'].isin(list1)

If numeric are stored like strings e.g. '27' convert first values to numeric by to_numeric with errors='coerce' for convert strings to NaN, so then replace them by original values by Series.fillna:

print (df)
     A
0   E2
1   27
2   99
3  NaN
4   20
5   14

print (df['A'].apply(type))
0      <class 'str'>
1      <class 'str'>
2      <class 'str'>
3    <class 'float'>
4      <class 'str'>
5      <class 'str'>
Name: A, dtype: object

list1 = [14, 61, 27, 82, 79, 75, 44, 10, 'E2','E9']

df['B'] = pd.to_numeric(df['A'], errors='coerce').fillna(df['A']).isin(list1)
print (df)
     A      B
0   E2   True
1   27   True
2   99  False
3  NaN  False
4   20  False
5   14   True

Here is a way to do it with np.where

df1 = pd.DataFrame({'A':['E2',27,99,np.nan,20,14]})
list1 = [14, 61, 27, 82, 79, 75, 44, 10, 'E2','E9']

_condition = df1.A.isin(list1)
df1['B'] = np.where(_condition,True,False)
df1

enter image description here

Related