python - 列包含列
问题描述
我想看看我的数据框列 A 的每一行中是否包含列 B 中的值。
df = pd.DataFrame({'A': ["Is it 54321?", "Is it 4321?", "Is it 321?"],
'B': [54321, 54321, 54321]})
我试过:
df["C"] = df["A"] .str .contains(df["B"])
我想:
'C': [1,0,0]
但我得到了:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
解决方案
或者:
df['C']=df.A.str.contains(r'\b(?:{})\b'.format('|'.join(df.B.astype(str)))).astype(int)
print(df)
A B C
0 Is it 54321? 54321 1
1 Is it 4321? 54321 0
2 Is it 321? 54321 0
推荐阅读
- css - 显示我的移动优先设计时出现问题
- python - 如何使用给定范围的值部分填充 numpy 数组?
- c# - RestRequestAsyncHandle.Abort() 不会触发 CancellationToken.IsCancellationRequested
- python - 我最多可以更改多少次按钮的背景?
- python - 两个 3D 张量之间的点积
- c - 如何从 VM 写入 azure 中启动诊断的串行日志?
- javascript - 如何使用纯 JavaScript 在给定时间内增加或减少进度条宽度?
- php - 从字符串中删除非 HTML 标签
- ios - 即使客户端 id 是正确的,它在授权回调中给出无效的 client_id 消息
- sql - 根据与要加入的值关联的列加入不同的表?