my input:
JavaScript
x
6
1
task1 = pd.DataFrame({'task1': ['|ReviewNG-Cecum Landmark','|ReviewNG-Cecum Landmark','|Cecum Landmark','|Cecum Landmark','|Cecum Landmark','|Cecum Landmark','|ReviewNG-Cecum Landmark',
2
'|ReviewNG-Cecum Landmark','|Other','|Other','|Other']})
3
task2 = pd.DataFrame({'task2': ['|Cecum Landmark|Other','|Cecum Landmark|Other','|Cecum Landmark|Other','|Cecum Landmark|Other','|Other','|Other','|Other',
4
'|Other']})
5
df = pd.concat([task1, task2], join = 'outer', axis = 1)
6
I trying get over df
all range where matching value is true
.
My code:
JavaScript
1
4
1
pat = "|Cecum Landmark|||Cecum Landmark"
2
idx = df.apply(lambda x: x.str.contains(pat, regex=True),axis=1)
3
idx.index[idx['task1']== True].tolist()
4
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?
Advertisement
Answer
Try the following,
JavaScript
1
6
1
result = df.apply(
2
lambda x: x.index[x.str.contains(pat, regex=True, na=False)].tolist(),
3
axis=0,
4
result_type="reduce",
5
).tolist()
6