如何用任意值检查序列中的值?

2024-06-28 20:44:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在和熊猫一起工作。我需要根据其他列中的条件在dataframe中创建一个新列。如果序列中包含一个值(返回文本的条件),我会尝试查找该序列中的每个值。当值完全相同时,这会起作用,但当值只是序列值的一部分时,则不会起作用

if any("something") in df2["Symptom"]:
    print("yes")
else:
    print("no")

我得到了一个yes,但是any(“某物”)也直接返回yes,在df2[“症状”]中没有检查,它总是正确的

另外,.str.contains()根本不起作用,我得到了错误:

Attribute error, attributeerror 'str' object has no attribute 'str'

我不知道为什么,因为df2[“症状”]是一个系列

谢谢你的帮助

编辑:示例数据:https://raw.githubusercontent.com/srpjv/data/main/name.csv

职能:

df2[“开发服务”]=df2.swifter.apply(条件,axis=1)

条件不变:

def条件(df2):

# Services 
if df2["Trigram MyPortal"] =="fr") is True return "Something"                                                                                       

# Autoconso                                                                                 
elif (df2["Trigram MyPortal"]=="Be")  is True: return "* Autoconso"                                                                                       
elif (df2["Trigram MyPortal"]=="De") is True: return "* Autoconso DE"                                      

Tags: truereturnifisany序列条件trigram