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

John Doe

In addition on my previous question Search for value in all DataFrame columns (except first column !) and add new column with matching column name (where I used a static keyword)

I'd like to check if the string in the first column is contained in one of the another columns in the same row and then add a new column with the matching column name(s). All columns names of all matched values!

Now i'm using this with a static keyword:

keyword='123'
f = lambda row: row.apply(str).str.replace(".","").str.contains(keyword ,na=False, flags=re.IGNORECASE)
df1 = df.iloc[:,1:].apply(f, axis=1)

df.insert(loc=1, column='Matching_Columns', value=df1.dot(df.columns[1:] + ', ').str.strip(', '))

Sample:

Input:

key | col_B | col_C | col_D | col_E
------------------------------------
123 | abcd  | 12345 | fght  | 7890
567 | tdfe  | 6353  | 0567  | 56789

Output:

key | match       | col_B | col_C | col_D | col_E
-------------------------------------------------
123 | col_C       | abcd  | 12345 | fght  | 7890
567 | col_D,col_E | tdfe  | 6353  | 0567  | 56789

Any help much appreciated!

Vishnudev
>>> df
  to_find col1 col2
0       a   ab   ac
1       b   aa   ba
2       c   bc   ee
>>> df['found_in'] = df.apply(lambda x: ' '.join(x.iloc[1:][x.iloc[1:].str.contains(str(x['to_find']))].index), axis=1)
>>> df
  to_find col1 col2   found_in
0       a   ab   ac  col1 col2
1       b   aa   ba       col2
2       c   bc   ee       col1

For better readability,

>>> def get_columns(x):
...     y = x.iloc[1:]
...     return y.index[y.str.contains(str(x['to_find']))]
... 
>>> df['found_in'] = df.apply(lambda x: ' '.join(get_columns(x)), axis=1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Python - Is one string column in another?

Replace a string form one column in another column

add column with string length of another column and cumsum?

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

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

How to check if value of one column is in another column of another row

Check if there is a similar string in the same column

Add column with column name as input string

Matching rownames with a string contained within a column

complicated list column to column string matching and deriving another column

create a new column based on string of another column

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

Formula to check if rows that have the same value in one column have matching values in another column

Matching a value in one column to the name of another column

Column number of matching string in column

pyspark: create column based on string contained in another column

Replace Matching Row String From Another Column

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

Check if string is in another column pandas

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

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

Append column value if string is contained in another string

How to add new string in column row

multiply row from one dataframe with matching column in another dataframe and add

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

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

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

check column has the same string