0

I have df with n columns (named as col'n') and 'name' column, I want to loc any row with col'n' have any keywords in 'searchkey' array.

It seems the list comprehension cannot apply in df column names list.

df[[c for c in df.columns if c[3:] = 'Col'].str.isin(searchkey))

Also, I tried to use apply function, but still face the problem.

df = pd.DataFrame({'name':['AA','BB','CC','DD','EE','FF'],
                   'col1':['mn','mxn','ca','sd','xa','ac'], 
                   'col2':['m','naa','x','ddn','q','y'],
                   'col3':['mn','mddn','csfd','sad','xxa','aad'], 
                   ... ... 
                   'coln':['sfn','mxc','cxa','sxxd','xada','axxc'],                    
})

searchkey = ['xx', 'aa', 'dd']

def func(x):
    return [x.columns] in searchkey  ## Error

df.apply(func, axis=1)

Any help is greatly appreciated!

1
  • What is the expected output, a list of columns or the rows? And is this a exact match, or a substring match? Commented Jul 17, 2019 at 15:56

1 Answer 1

3

Try this

df = df[df.isin(search_key).any(1)]
Sign up to request clarification or add additional context in comments.

1 Comment

no actually it is mentioned in question I want to loc any row with col'n' have any keywords in 'searchkey' array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.