【发布时间】:2019-09-16 19:20:04
【问题描述】:
我想检查一列是否包含来自其他列的值,并用 True 或 False 填充第三列。
df 输入:
id | name | account
-------------------
01 | John | AB01
02 | Emma | AB03
03 | Alice | AB03
df 输出:
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:“系列”对象没有“包含”属性
df['match'] = np.where(df['account'].str.contains(df['id']), 'True','False')
错误:TypeError:“系列”对象是可变的,因此它们不能被散列
非常感谢任何帮助!
【问题讨论】:
-
str.contains()采用字符串变量而不是系列。我知道的方式是你可以使用 for 循环来循环你拥有的所有 id...但我很确定还有其他更好的方法来做到这一点
标签: python python-3.x pandas