4

I have a pandas dataframe like this:

enter image description here

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'])
itzy
  • 11,275
  • 15
  • 63
  • 96
  • won't `df.apply(lambda x: x['city'].lower() in x['citylist'], axis=1)` work?, your version iterates over each column in turn, you need to pass `axis=1` to iterate row-wise – EdChum Aug 26 '15 at 17:36

1 Answers1

7

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]
EdChum
  • 376,765
  • 198
  • 813
  • 562