Is there a way to find a string in the DataFrame and return the column names upon match.
In the below example, I am trying to find the columns where "SRC" appears, not sure if I am close, but it returns all the column names instead of only the relevant ones. I 'm sure I am doing something silly.
df = pd.DataFrame({'col1':['foo SRC','bar','baz'], 'col2':['foo','bar','baz'],'col3':['SRC','bar','SRC'],
'col4':['SRC','SRC','SRC']})
df['col_list']= '/'.join(df.apply(lambda x : x.str.contains('SRC')).any().loc[lambda x : x].index)
Actual Result:
---------------------------------------------
col1 |col2 |col3 |col4 |col_list
--------|-------|-------|-------|----------------
foo SRC |foo |SRC |SRC |col1/col3/col4
bar |bar |bar |SRC |col1/col3/col4
baz |baz |SRC |SRC |col1/col3/col4
Expected:
col1 |col2 |col3 |col4 |col_list
--------|-------|-------|-------|----------------
foo SRC |foo |SRC |SRC |col1/col3/col4
bar |bar |bar |SRC |col4
baz |baz |SRC |SRC |col3/col4