我正在和熊猫一起工作。我需要根据其他列中的条件在数据框中创建一个新列。我尝试查找系列中的每个值,如果它包含一个值(返回文本的条件)。当值完全相同时,这有效,但当该值只是系列值的一部分时,则无效。
if any("something") in df2["Symptom"]:
print("yes")
else:
print("no")
我得到了一个 yes,但是 any("something") 也直接返回一个 yes,它总是正确的,无需检查 df2["Symptom"]。
另外, .str.contains() 根本不起作用,我收到错误:
Attribute error, attributeerror 'str' object has no attribute 'str'
我不知道为什么,因为 df2["Symptom"] 是一个系列。
感谢您的帮助
编辑:样本数据:https://raw.githubusercontent.com/srpjv/data/main/name.csv
功能 :
df2["减值服务"] = df2.swifter.apply(条件, axis=1)
条件不变:
定义条件(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"
Use numpy.where with Series.str.contains:
df2['new'] = np.where(df2["Symptom"].str.contains('something'), 'yes', 'no')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)