【问题标题】:Count Match value in DataFrame based on list根据列表计算DataFrame中的匹配值
【发布时间】:2021-11-09 20:29:37
【问题描述】:

我有一个数据框,其中有一些项目标题,例如

ratings_dict = {
    "TYPE": ["Testing","Headphone","Iphone","AC","Laptop","Monitor"],
}

df = pd.DataFrame(ratings_dict)

想要根据给定的列表计算值:

Search_history=['test','phone','lap','testing','tes','iphone','Headphone','head','Monitor','ac']

预期输出:

注意:在这种情况下,单词“phone”与数据帧“Headphone”和“Iphone”中的 2 个值匹配,然后 Count 将同时递增。

任何建议或代码 sn-p 都会有所帮助。

【问题讨论】:

  • 请不要在您的问题中使用图片:它们不可搜索,也不能复制粘贴。创建可复制粘贴的代码,例如,您的示例数据框可以包含创建它所需的代码。
  • “任何建议或代码 sn-p 都会有所帮助。”:自己已经尝试过什么?这有助于我们更好地指导您。
  • pandas.pydata.org/docs/reference/api/… 可能会有所帮助。
  • 您好,感谢您的回复,我已经尝试过df.str.contains 方法但无法获得匹配数。

标签: python python-3.x list dataframe string-matching


【解决方案1】:

您需要将所有内容都转换为小写,然后计算 TYPE 是搜索历史项的子字符串的次数,反之亦然

import pandas as pd

ratings_dict = {
    "TYPE": ["Testing","Headphone","Iphone","AC","Laptop","Monitor"],
}
df = pd.DataFrame(ratings_dict)

Search_history=['test','phone','lap','testing','tes','iphone','Headphone','head','Monitor','ac']

# convert everything to lower case
Search_history = [ x.lower() for x in Search_history]
df['TYPE'] = [ x.lower() for x in df.TYPE]

# count up the number of times one of the TYPEs is a substring of a Search_history or a Search_history is a substring of a TYPE
df['count'] = [ sum( x in y or y in x for y in Search_history) for x in df.TYPE]

【讨论】:

    【解决方案2】:

    由你来定义什么条件是有意义的,你的问题有点太松散了。您可以检查值是否匹配,也可以在检查之前将一些列表值转换为默认值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2017-03-07
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多