我有一个包含以下行的发布数据框:
出版物编号、标题、作者姓名、日期 12344,设计风格,Jake Kreath,20071208 12334,为什么的力量,萨曼莎·芬恩,20150704
我向用户请求一个字符串,并使用该字符串搜索标题
目标:搜索数据框以查看标题是否包含用户提供的单词,并返回新数据框中仅包含标题和发布标识的行
这是我目前的代码:
import pandas as pd
from pandas import DataFrame
publications = pd.read_csv(filepath, sep= "|")
search_term = input('Enter the term you are looking for: ')
def stringDataFrame(publications, title, regex):
newdf = pd.DataFrame()
for idx, search_term in publications['title'].iteritems():
if re.search(regex, search_term):
newdf = concat([publications[publications['title'] == search_term], newdf], ignore_index=True)
return newdf
print(newdf.stringDataFrame)
使用
.str.contains
和.loc
的组合小心点,因为如果你的标题是
'nightlife'
,有人搜索'night'
,这将返回一个匹配项。如果这不是你想要的行为,那么你可能需要.str.split
正如jpp指出的,
str.contains
是区分大小写的。一个简单的解决方法就是确保所有内容都是小写的现在
Lord
、LoRD
、lord
和所有其他排列都将返回有效匹配,并且原始DataFrame
的大小写保持不变完整的例子,但你应该接受@ALollz上面的答案
根据您的错误,您可以使用下面的新代码作为逻辑的一部分过滤掉所有
np.nan
值:相关问题 更多 >
编程相关推荐