Why I can't get the correct mask columns in pandas

Viewed 30

For example, if I have a data frame

    x   f
0   0   [0, 1]
1   1   [3]
2   2   [2, 3, 4]
3   3   [3, 6]
4   4   [4, 5]

If I want to remove the rows which columns x doesn't in f columns, I tried with where and apply but I can't get the expected results. I got the below table and I want to know why row 0,2,3 are 0 instead of 1?

    x   f   mask
0   0   [0, 1]  0
1   1   [3] 0
2   2   [2, 3, 4]   0
3   3   [3, 6]  0
4   4   [4, 5]  0

Anyone knows why? And should I do to handle this number vs list case?

df1 = pd.DataFrame({'x': [0,1,2,3,4],'f' :[[0,1],[3],[2,3,4],[3,6],[3,5]]}, index = [0,1,2,3,4])
df1['mask'] = np.where(df1.x.values in df1.f.values ,1,0)
2 Answers

Here is necessary test values by pairs - solution with in in list comprehension:

df1['mask'] = np.where([a in b for a, b in df1[['x', 'f']].values],1,0)

Or with DataFrame.apply and axis=1:

df1['mask'] = np.where(df1.apply(lambda x: x.x in x.f, axis=1),1,0)

print (df1)
   x          f  mask
0  0     [0, 1]     1
1  1        [3]     0
2  2  [2, 3, 4]     1
3  3     [3, 6]     1
4  4     [3, 5]     0

IIUC row explode then use isin

pd.DataFrame(df1.f.tolist()).isin(df1.x).any(1).astype(int)
Out[10]: 
0    1
1    0
2    1
3    1
4    0
dtype: int32
df1['mask'] = pd.DataFrame(df1.f.tolist()).isin(df1.x).any(1).astype(int)
Related