pandas - 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.
Desired output would just be the index/indices of the entry/entries that contain the word I am looking for.
解决方案
针对您的情况的一个简单技巧,如果您只需要检查包含,则将 bag 列视为字符串而不是列表
df['word_flag'] = df['bag'].astype(str).str.contains('your-word-here')
如果您特别需要索引
df[df['word_flag'] == 1].index
推荐阅读
- ios - 如何在不初始化 UIWebView 或 WKWebView 的情况下获取 WKWebView 的 UserAgent?
- bash - Bash:需要替换文件中的不同电子邮件地址
- php - Laravel 数据表搜索包括数据透视表结果
- python-3.x - 返回一个不冲突的选择
- azure - MS Graph Guest user cannot read Azure AD data
- php - 如果属性不存在则显示空字符串
- python - Python,Pandas 从数据框创建新数据
- javascript - window.open 中的 PHP Ajax 响应
- python - 在 python 中使用正则表达式在字符串中查找精确长度匹配
- c++ - 为什么按值传递 string_view?为什么 Visual Studio 不能对此进行优化?