Reputation: 501
my input:
task1 = pd.DataFrame({'task1': ['|ReviewNG-Cecum Landmark','|ReviewNG-Cecum Landmark','|Cecum Landmark','|Cecum Landmark','|Cecum Landmark','|Cecum Landmark','|ReviewNG-Cecum Landmark',
'|ReviewNG-Cecum Landmark','|Other','|Other','|Other']})
task2 = pd.DataFrame({'task2': ['|Cecum Landmark|Other','|Cecum Landmark|Other','|Cecum Landmark|Other','|Cecum Landmark|Other','|Other','|Other','|Other',
'|Other']})
df = pd.concat([task1, task2], join = 'outer', axis = 1)
I trying get over df
all range where matching value is true
.
My code:
pat = "\|Cecum Landmark\||\|Cecum Landmark"
idx = df.apply(lambda x: x.str.contains(pat, regex=True),axis=1)
idx.index[idx['task1']== True].tolist()
What I get:*
[2, 3, 4, 5]
So its correct, but how to get all index over each column in df? In other word, I dont want input manually name of column to get index matching value, but get ouptut per column.
So what I expect:
[2,3,4,5] [0,1,2,3]
so for example two lists(or more) each for its own column. There might be a better way to find matches and get the index?
Upvotes: 0
Views: 28
Reputation: 2757
Try the following,
result = df.apply(
lambda x: x.index[x.str.contains(pat, regex=True, na=False)].tolist(),
axis=0,
result_type="reduce",
).tolist()
Upvotes: 1