python - How to filter a pandas column by list of strings?
问题描述
The standard code for filtering through pandas would be something like:
output = df['Column'].str.contains('string')
strings = ['string 1', 'string 2', 'string 3']
Instead of 'string' though, I want to filter such that it goes through a collection of strings in list, "strings". So I tried something such as
output = df['Column'].str.contains('*strings')
This is the closest solution I could find, but did not work How to filter pandas DataFrame with a list of strings
Edit: I should note that I'm aware of the | or operator. However, I'm wondering how to tackle all cases in the instance list strings is changing and I'm looping through varying lists of changing lengths as the end goal.
解决方案
You can create a regex string and search using this string.
Like this:
df['Column'].str.contains('|'.join(strings),regex=True)
推荐阅读
- sql - 试图从sql server中的列中提取文件名
- java - 如何修复异常“org.omg.CORBA.MARSHAL: Buffer overread by 4 bytes”
- xcode - 保持 UIBarButtonItem 的 UIImage 的原始颜色
- java - 无效锁中断()
- android - ObservableField 的使用
Android数据绑定中的字符串 - c# - MS VSTO 插件的 _Document.GetCrossReferenceItems(Object) 中可能存在错误?
- python - Oracle WHERE 子查询中的多列“cx_Oracle.DatabaseError:ORA-00920:无效的关系运算符”
- python - 使用python生成随机json数据
- openssl - Fabric 1.4:握手失败,出现致命错误 SSL_ERROR_SSL:错误:1408F10B:SSL 例程:SSL3_GET_RECORD:错误的版本号
- mysql - 从列中提取子字符串并检查该子字符串是否与其他子字符串匹配