【问题标题】:Exact matching string with "==" operator between Str and a list of strings在 Str 和字符串列表之间使用“==”运算符精确匹配字符串
【发布时间】:2022-01-05 23:28:30
【问题描述】:

我有这个例子 df:

df6 = pd.DataFrame({
                   'answer1': ['Lo', 'New York', 'Toronto'],
                   'answer2': ['London', 'New', 'Paris'],
                   'answer3': ['CA', 'CA', 'CA'],
                   'correct': [['London'], ['New York'], ['Toronto']]
                   })

df6

给予:

    answer1   answer2     answer3     correct
0   Lo         London         CA    [London]
1   New York    New           CA    [New York]
2   Toronto    Paris          CA    [Toronto]

我正在尝试通过匹配str 格式的值,在名为Answer 的新列中获取包含正确列中文本的列名称(答案1 或2 .. 等)。正确的列具有列表类型的数据

我使用了以下代码:

cols = df6.filter(like='answer').columns

df6['Answer'] = df6[cols].apply(lambda s: ', '.join(cols[(m:=[str(s[col]) in str(df6.loc[s.name, 'correct']) for col in cols])]) , axis=1)

但我得出的结果不准确:

    answer1    answer2     answer3     correct       Answer
0   Lo         London       CA         [London]      answer1, answer2
1   New York   New          CA         [New York]    answer1, answer2
2   Toronto    Paris        CA         [Toronto]     answer1

应该是:

    answer1    answer2     answer3     correct       Answer
0   Lo         London       CA         [London]      answer2
1   New York   New          CA         [New York]    answer1
2   Toronto    Paris        CA         [Toronto]     answer1

如果我将in 更改为==,代码将不起作用,因为数据类型不可比较(str 与列表),而且我需要将列表项包装在str 中以避免出现多个数据问题我原来的df

不知道怎么实现?


编辑:

所以为了这行代码,让它有点短:

df6['Answer'] = df6[cols].apply(lambda s: ', '.join(cols[(m:=[str(s[col]) in str(df6.loc[s.name, 'correct']) for col in cols])]) , axis=1)

上一行中的这个特定部分:

str(s[col]) in str(df6.loc[s.name, 'correct'])

我需要找到一种方法来检查完全匹配以使用== 而不是in。这就像遍历字符串列表

我试着玩它但没有结果:

 str(s[col]) == [x for x in str(df6.loc[s.name, 'correct'])]

有什么想法吗?

【问题讨论】:

    标签: python python-3.x pandas string list


    【解决方案1】:

    去除正确的角括号,检查df中是否存在,然后有条件地复制列

     df6['answer'] =df6.isin(df6['correct'].str[0].to_list()).agg(lambda s: s.index[s].values, axis=1)
    df6
    
    
    
         answer1 answer2 answer3     correct     answer
    0        Lo  London      CA    [London]  [answer2]
    1  New York     New      CA  [New York]  [answer1]
    2   Toronto   Paris      CA   [Toronto]  [answer1]
    

    【讨论】:

    • 感谢 wwnde,它运行良好,但有时正确的列可能有多个匹配值。 df6 = pd.DataFrame({ 'answer1': ['Lo', 'New York', 'Toronto'], 'answer2': ['London', 'New', 'Paris'], 'answer3': ['CA', 'CA', 'CA'], 'correct': [["'London'","'CA'"], ['New York'], ['Toronto']] })
    • 这样的? df6.isin(df6['correct'].explode().str.replace('[^\w]','', regex=True).to_list()).agg(lambda s: s.index[s].values, axis=1)
    • 它只为第一行提供正确的结果,但对其他两行的匹配不准确。
    【解决方案2】:

    我认为您应该查看answer 列中的元素是否在列表中,而不是在字符串中,在correct 列中:

    df6['Answer'] = df6[cols].apply(lambda s: ', '.join(cols[(m:=[str(s[col]) in list(df6.loc[s.name, 'correct']) for col in cols])]) , axis=1)
    

    应该可以,因为这是检查answerX 元素是否在correct 列表中。

    【讨论】:

    • 是的,它通过检查元素是否在正确的列表中来工作,但它没有给出完全匹配。查看前两行的输出。
    • 现在,检查这段代码的输出
    猜你喜欢
    • 2017-10-28
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多