python - 检查一列中的值是否在另一列的列表中

标签 python pandas

我有一个像这样的 pandas 数据框:

enter image description here

我需要知道city是否出现在citylist的列表中(忽略大小写)。

我尝试像这样使用apply,但无法找出正确的语法:

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

最佳答案

传递参数axis=1 apply 逐行迭代:

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]

关于python - 检查一列中的值是否在另一列的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233069/

相关文章:

Python3 : Iterating a binary string turns character into integer

python - 跨多处理 python 共享 pandas 数据框字典

python-3.x - 使用开始和结束列在同一 Pandas 数据框中有效合并重叠间隔

python - 为什么我应该制作数据框的*浅*副本?

python - 如何从该字典中获取 'Last' 值?

python - 玩家应该继承还是拥有关卡?

python - Pandas 直方图的权重

python - 如果是空格,如何在不删除分隔符的情况下进行拆分

python - 提取编号时为 NaN。 pandas 两个日期之间的天数

python - pandas 插值不适用于日期索引