df.x.isin(df.y)
checks for each element x
, eg 0
, is equal to some of the values of df.y
, eg is 0
equal to [0,2]
, no, and so on.
With this, you can just do a for loop:
df[ [x in y for x,y in zip(df['x'], df['y'])] ]
Let us try explode
with index loc
out = df.loc[df.explode('y').query('x==y').index.unique()]
Out[217]:
x y
0 0 [0, 2]
2 2 [2, 3]
3 3 [3, 4]
Just an other solution:
result = (
df
.assign(origin_y = df.y)
.explode('y')
.query('x==y')
.drop(columns=['y'])
.rename({'origin_y': 'y'})
)
x y
0 0 [0, 2]
2 2 [2, 3]
3 3 [3, 4]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.