3
key_words_to_search = ['hello', 'goodbye']  
df = pd.DataFrame({
'col1':['hello','hi','ciao'],
'col2':['hello panda','goodbye','bonjour'],
'col3':['ni hao','hola','hello']})

I've been using something like the below, but not sure how to get the actual name of the column. Thanks!

mask = df.applymap(lambda x: word in str(word).lower())
temp = df[mask.any(axis=1)].copy() 

Tabular visualization of the data frame

2 Answers 2

2

Here's a way of doing:

d = []

for k in key_words_to_search:
    print(k)
    i = df.applymap(lambda x: k in x)
    i = i.astype(int).mask(i, i.columns.to_series(), axis=1).astype(str).agg(lambda x: ','.join(i for i in x if not i.isdigit()), 1)
    d.append(i)

df[['hello','goodbye']] = pd.concat(d, axis=1)

print(df)

          col1         col2    col3      hello goodbye
0        hello  hello panda  ni hao  col1,col2        
1  hello panda      goodbye    hola       col1    col2
2       ni hao      goodbye   hello       col3    col2
Sign up to request clarification or add additional context in comments.

1 Comment

I changed it to: i = df_test.applymap(lambda x: k in str(x).lower()). But otherwise works great. thanks!
1
import pandas as pd
key_words_to_search = ['hello', 'x']  
df = pd.DataFrame({
'col1':['hello','hi','ciao', 'x'],
'col2':['hello panda','goodbye','bonjour', 'f'],
'col3':['ni hao','hola','hello', 'a']})
cols = list(map(lambda col_name: (col_name, any(x in df[col_name].unique() for x in key_words_to_search)), list(mask.columns)))
res = list(map(lambda item: item[0], list(filter(lambda c: c[1], cols))))
print(res)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.