如何使用Python DataFrame检查B列中是否包含A列的内容? [英] How to check whether the content of Column A is contained in Column B using Python DataFrame?

查看:1146
本文介绍了如何使用Python DataFrame检查B列中是否包含A列的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫DataFrame中有两列:authorsname.我想创建第三列:如果相应行的authors中包含相应行的name,则单元格的值为True,否则为False.

因此结果将如下图所示.

我尝试了.str.contains().str.extract().str.find().where()等. 但是Python返回了一个错误:系列"对象是可变的,因此它们不能被散列. 有谁知道如何在Python中创建第三列?

解决方案

IIUC,则可以 解决方案

IIUC then you can apply a lambda row-wise to check if the Name string is present in Authors:

df['Check'] = df.apply(lambda row: row['Name'] in row['Authors'], axis=1)

should work

You can't use str.contains(), str.extract(), str.find(), or where()here because you're trying to compare row-wise and those methods expect a fixed list or pattern for the searching criteria.

这篇关于如何使用Python DataFrame检查B列中是否包含A列的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆