简体   繁体   中英

Check if a value in one column is in a list in another column

I have a pandas dataframe like this:

在此处输入图片说明

and I need to know if city appears in the list in citylist (ignoring case).

I've tried to use apply like this, but can't figure out the correct syntax:

df.apply(lambda x: x['city'].lower() in x['citylist'])

Pass param axis=1 to apply to iterate row-wise:

In [49]:

df[df.apply(lambda x: x['city'].lower() in x['citylist'], axis=1)]
Out[49]:
          city                         citylist
1       RESTON                [reston, herndon]
4  SPRINGFIELD  [springfield, west springfield]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM