我想检查一列是否包含另一列的值,并用True或False填充第三列。你知道吗
数据框输入:
id | name | account
-------------------
01 | John | AB01
02 | Emma | AB03
03 | Alice | AB03
数据框输出:
id | name | account | match
----------------------------
01 | John | AB01 | True
02 | Emma | AB03 | False
03 | Alice | AB03 | True
我试过这个:
df['match'] = np.where(df['account'].contains(df['id']), 'True','False')
错误:AttributeError:“Series”对象没有“contains”属性
df['match'] = np.where(df['account'].str.contains(df['id']), 'True','False')
错误:TypeError:“Series”对象是可变的,因此不能对它们进行哈希运算
非常感谢您的帮助!你知道吗
像这样的?你知道吗
对于测试,如果每行包含值,则使用
apply
和in
对于布尔值
True, False
:对于字符串
'True', 'False'
:编辑:
缺少值,因此可能的解决方案是使用
np.nan == np.nan
是False
,因此添加了if-else
语句:对于布尔值
True, False
:对于字符串
'True', 'False'
:另一个想法是使用带有
try-exception
语句的自定义函数:对于布尔值
True, False
:对于字符串
'True', 'False'
:相关问题 更多 >
编程相关推荐