首页 > 解决方案 > Grabbing an unordered index given a matching criteria

问题描述

I have a pandas data frame that is already filtered so the indices are not in order (i.e not from 0 - the end of my data frame). I have a column that is a 'bag of words'. This is simply a list of words. I know the word I am searching for. How can I find the index/indices that contain this word?
I tried using the '.index' function but I may not be using it correctly.

Sample of DataFrame

Desired output would just be the index/indices of the entry/entries that contain the word I am looking for.

标签: pandasdataframe

解决方案


针对您的情况的一个简单技巧,如果您只需要检查包含,则将 bag 列视为字符串而不是列表

df['word_flag'] = df['bag'].astype(str).str.contains('your-word-here')

如果您特别需要索引

df[df['word_flag'] == 1].index

推荐阅读