Python Pandas: Check if string in one column is contained in string of another column in the same row

swang16 :

I have a dataframe like this:

RecID| A  |B
----------------
1    |a   | abc 
2    |b   | cba 
3    |c   | bca
4    |d   | bac 
5    |e   | abc 

And want to create another column, C, out of A and B such that for the same row, if the string in column A is contained in the string of column B, then C = True and if not then C = False.

The example output I am looking for is this:

RecID| A  |B    |C 
--------------------
1    |a   | abc |True
2    |b   | cba |True
3    |c   | bca |True
4    |d   | bac |False
5    |e   | abc |False

Is there a way to do this in pandas quickly and without using a loop? Thanks

jezrael :

You need apply with in:

df['C'] = df.apply(lambda x: x.A in x.B, axis=1)
print (df)
   RecID  A    B      C
0      1  a  abc   True
1      2  b  cba   True
2      3  c  bca   True
3      4  d  bac  False
4      5  e  abc  False

Another solution with list comprehension is faster, but there has to be no NaNs:

df['C'] = [x[0] in x[1] for x in zip(df['A'], df['B'])]
print (df)
   RecID  A    B      C
0      1  a  abc   True
1      2  b  cba   True
2      3  c  bca   True
3      4  d  bac  False
4      5  e  abc  False

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if string in one column is contained in string of another column in the same row and add new column with matching column name

Pandas dataframe: Check if regex contained in a column matches a string in another column in the same row

Pandas: check if string value in one column is part of string of another column in same row of dataframe - current script returning all Yes

Check if string is in another column pandas

Mysql check if column in a row is contained in another column?

Python - Is one string column in another?

One to multiple merge two dataframes if one column string contained in another with Python

How to check string of one column ,and change of string of another column using pandas

Append column value if string is contained in another string

Pandas: Determine if a string in one column is a substring of a string in another column

Remove string from one column if present in string of another column pandas

Pandas - For each group, if string in one column is in another column, add to column

Check if at least one column contains a string in pandas

How do I replace all of one character string in one column with another column's character string in the same row in R?

How to drop entire row if string of one column contains the word from another column in pandas dataframe

Check if string in a column, then return value from another column at the same index

Pandas / Check if a string is in a column

How to combine string from one column to another column at same index in pandas DataFrame?

pandas dataframe check if column contains string that exists in another column

Find if string is contained in a column of the same mysql table

How to check if values in one dataframe column are contained in another entire column?

If partial string in the same pandas column match then update the value in another column

Check if string-sliced characters in Pandas column contain another string

python pandas - check if a string type exists in a column

Is there a way in pandas to filter rows in a column that are contained in a string

Pandas DF Select Column Contained By String

Groupby one column if string contained and get maximum values of another column in R

check column has the same string

Check if there is a similar string in the same column