Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

pandas - How to check whether the content of Column A is contained in Column B using Python DataFrame?

I have two columns in a pandas DataFrame: authors and name. I want to create a third column: a cell's value is True if the corresponding row's name is contained in the corresponding row's authors, and False otherwise.

So the result will look like the picture below.

enter image description here

I have tried .str.contains(), .str.extract(), .str.find(), .where(), and etc. But Python returns an error: 'Series' objects are mutable, thus they cannot be hashed. Does anyone know how to create the third column in Python?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...