如何检查任何一个系列中的值?
How to check value in a series with any?
我正在与 Pandas 合作。我需要根据其他列中的条件在数据框中创建一个新列。我尝试查找系列中的每个值,如果它包含一个值(return 文本的条件)。这在值完全相同但不是当值只是值的一部分时有效系列。
if any("something") in df2["Symptom"]:
print("yes")
else:
print("no")
我得到了肯定,但是任何(“某事”)也 return 直接得到肯定,没有检查 df2["Symptom"].
总是正确的
此外,.str.contains() 根本不起作用,我得到了错误:
Attribute error, attributeerror 'str' object has no attribute 'str'
我不知道为什么,因为 df2["症状"] 是一个系列。
感谢您的帮助
编辑:样本数据:https://raw.githubusercontent.com/srpjv/data/main/name.csv
函数:
df2["Deversement 服务"] = df2.swifter.apply(条件,轴=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"
使用numpy.where
with Series.str.contains
:
df2['new'] = np.where(df2["Symptom"].str.contains('something'), 'yes', 'no')
我正在与 Pandas 合作。我需要根据其他列中的条件在数据框中创建一个新列。我尝试查找系列中的每个值,如果它包含一个值(return 文本的条件)。这在值完全相同但不是当值只是值的一部分时有效系列。
if any("something") in df2["Symptom"]:
print("yes")
else:
print("no")
我得到了肯定,但是任何(“某事”)也 return 直接得到肯定,没有检查 df2["Symptom"].
总是正确的此外,.str.contains() 根本不起作用,我得到了错误:
Attribute error, attributeerror 'str' object has no attribute 'str'
我不知道为什么,因为 df2["症状"] 是一个系列。
感谢您的帮助
编辑:样本数据:https://raw.githubusercontent.com/srpjv/data/main/name.csv
函数:
df2["Deversement 服务"] = df2.swifter.apply(条件,轴=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"
使用numpy.where
with Series.str.contains
:
df2['new'] = np.where(df2["Symptom"].str.contains('something'), 'yes', 'no')