我有一个pandas数据框,其中一列是文本描述字符串。我需要创建一个新的列来标识列表中的一个字符串是否在文本描述中
df = pd.DataFrame({'Description': ['2 Bedroom/1.5 Bathroom end unit Townhouse.
Available now!', 'Very spacious studio apartment available', ' Two bedroom, 1
bathroom condominium, superbly located in downtown']})
list_ = ['unit', 'apartment']
那么结果应该是
Description in list
0 2 Bedroom/1.5 Bathroom end unit Townhouse. Av... True
1 Very spacious studio apartment available True
2 Two bedroom, 1 bathroom condominium, superbly... False
我可以这样做
for i in df.index.values:
df.loc[i,'in list'] = any(w in df.loc[i,'Description'] for w in list_)
但是对于一个大的数据集,它需要的时间比我想的要长
通过使用
str.contains
使用
np.char.find
-相关问题 更多 >
编程相关推荐