【发布时间】:2021-09-02 09:08:57
【问题描述】:
我的问题是一种不同的方式,因为我们通常使用str.contains() 检查字符串。我想检查数据框中的子字符串是否包含在长字符串变量中。
数据框如下所示:
| Account | Substring | Category |
|---|---|---|
| 1001 | Cash Payment | Category #1 |
| 1002 | Credit Card Payment | Category #2 |
长字符串变量是long_str = “Cash Payment by Customer”。
那么,当使用.loc 搜索/过滤数据帧中的记录时,long_str 中包含的子字符串是否有类似str.contains() 的功能,但方式相反?
下面是我想尝试过滤数据帧的代码,除了 str.contains() 不起作用。谢谢!
df.loc[df[‘Substring’].str.contains(long_str)]
【问题讨论】:
标签: python pandas string dataframe contains