【问题标题】:LOC search string with AND condition in PythonPython中带有AND条件的LOC搜索字符串
【发布时间】:2021-09-27 03:23:48
【问题描述】:

我正在尝试使用带有 AND 条件的 LOC。它适用于 OR 条件,但当列中有重复值时,我无法让它与 AND 一起使用。

def locreplace(df,col,needle,replace,needle2=''):
   if (needle2==''):
      df.loc[df[col].str.contains(needle, case=False)==True,col] = replace
   else:
     df.loc[[df[col].str.contains(needle, case=False)==True] and df[col].str.contains(needle2, case=False)==True,col] = replace

这个没有重复的表按预期工作:

#Create a data frame
data = [['granny apple', 'juicy'], ['blood orange', 'refreshing'], ['spanish lemon', 'tangy']]
fruitdf = pd.DataFrame(data, columns = ['fruit', 'taste'])

#Single replace - works
#locreplace(fruitdf,'fruit','apple','big red nice apple')

#Will fail - works
#locreplace(fruitdf,'fruit','apple','big red apple','uncle')

#Double replace - works
locreplace(fruitdf,'fruit','apple','big huge red apple','granny')

但是,当您创建包含两个“granny”条目的数据框时,即使 AND 条件中的“apple”不匹配,双重替换 AND 条件也会替换“granny”的两个实例。

data = [['granny apple', 'juicy'], ['granny blood orange', 'refreshing'], ['spanish lemon', 'tangy']]
fruitdf = pd.DataFrame(data, columns = ['fruit', 'taste'])

#Single replace - works
#locreplace(fruitdf,'fruit','apple','big red nice apple')

#Will fail - works
#locreplace(fruitdf,'fruit','apple','big red apple','uncle')


#Double replace - fails
locreplace(fruitdf,'fruit','apple','big huge red apple','granny')


毫无疑问是我的错,以及括号放错(或对代码的误解),但是用 loc(或其他更简单的方法)替换 AND 条件的正确方法是什么?

当前输出:

    fruit   taste
0   big huge red apple  juicy
1   big huge red apple  refreshing
2   spanish lemon   tangy

期望的输出:

    fruit   taste
0   big huge red apple  juicy
1   granny blood orange refreshing
2   spanish lemon   tangy

【问题讨论】:

    标签: python pandas replace contains pandas-loc


    【解决方案1】:

    问题出在locreplace 中的else 块中

    [df[col].str.contains(needle, case=False)==True]
    

    这是一个在第一个索引中包含系列而不是系列的列表。您需要删除括号并将and替换为&

    df.loc[df[col].str.contains(needle, case=False) & df[col].str.contains(needle2, case=False), col] = replace
    

    输出

                     fruit       taste
    0   big huge red apple       juicy
    1  granny blood orange  refreshing
    2        spanish lemon       tangy
    

    【讨论】:

    • 谢谢家伙 - 这里很热,我今天过得很慢,感谢您的快速响应,第一次工作。
    猜你喜欢
    • 2012-03-14
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    相关资源
    最近更新 更多