简体   繁体   English

返回数据框中列上匹配值的索引

[英]return index of matching value over columns in dataframe

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 .我试图克服匹配值为df 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] [2、3、4、5]

So its correct, but how to get all index over each column in df?所以它是正确的,但是如何获取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] [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?可能有更好的方法来查找匹配项并获取索引?

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM