【问题标题】:Best solution on partial string search with Pandas使用 Pandas 进行部分字符串搜索的最佳解决方案
【发布时间】:2015-07-10 23:46:07
【问题描述】:

我使用非常大的数据集 (1.5gb+) 并对其进行部分字符串搜索。

我能够为我的工作编写一个脚本,但是时间太长了:

fhand = open('C:/Users/promotor/Documents/tce-sagres/TCE-PB-SAGRES-Empenhos_Esfera_Municipal.txt','r')
pergunta = raw_input('Pesquisa: ')
fresult = open('resultado.csv','w')
for line in fhand :
    #linha = linha + 0.001 
    #update_progress(int(linha)*1000)
    if pergunta in line : 
        print line
        fresult.write(line)  
print "terminado."""

我想知道在 Pandas 上是否有更快的方法来做到这一点。我尝试了 str.contains,但我只能搜索一列。我想知道是否有更快的方法。我尝试了“str.contains”,但只能搜索一列。

最好的问候。

【问题讨论】:

    标签: string search pandas row partial


    【解决方案1】:

    您正在迭代一个 for 循环,这可能需要花费大量时间。我建议将整个文件作为字符串读取,然后使用正则表达式来匹配您的模式。

    试试下面的代码,

    import re
    with open(your_file_name,'r') as f:
        lines=f.read()
    name = input('pattern :')
    pattern_to_match = r'(?<=\n).*%s.*(?=\n)'%name
    matched_pattern = re.findall(pattern_to_match, lines, re.IGNORECASE)
    print (matched_pattern)
    

    【讨论】:

    • 谢谢。但是文件太大(10gb csv),所以我决定把它放在一个 sqlite 数据库中并在那里搜索它。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2010-09-07
    • 2017-03-28
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多