【问题标题】:Filtering a dataset from a list of words从单词列表中过滤数据集
【发布时间】:2022-08-17 16:21:08
【问题描述】:

我正在对数据集列进行位置过滤。我正在使用类似的东西:

locations_list = [\"london\", \"new york\", \"berlin\", \"texas\"]

然后我使用过滤列

df[df[\"location\"].str.contains(r\'\\b(?:{})\\b\'.format(\'|\'.join(location_list))]

它可以工作,但它会选择像“新西兰”这样的地方,假设是因为它单独搜索新的。有没有办法解决这个问题?

标签: python arrays regex


【解决方案1】:

我会使用熊猫isin

df[df['location'].isin(locations_list)]

【讨论】:

  • 这是行不通的,因为我有例子说“伦敦,英国”,这不会算作匹配。有解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多