Home  >  Q&A  >  body text

用 python 给数据打标签,500 万条数据怎样提高效率?

ringa_leeringa_lee2763 days ago548

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-17 16:54:14

    So do you really use pandas as a tool for reading data?.

    Added a column is_tobacco as the mark you said

    filter_query returns a list containing these words, and the efficiency has been improved

    Secondly, you can split it and use multiprocessing to execute it. This will speed up the process by more than a little

    import pandas as pd
    word = pd.read_table('test.txt', encoding = 'utf-8', names = ['query'])
    
    def signquery(word):
        tobacco = [u'烟', u'白沙', u'黄金叶', u'利群', u'南京九五', u'黄鹤楼软',  u'黄鹤楼硬', u'娇子', u'钻石荷花', u'玉溪', u'七匹狼尚品',  u'七匹狼软灰']
        word['is_tobacco'] = word['query'].apply(lambda name:name in tobacco)
        return word
    
    def filter_query(word):
        tobacco = [u'烟', u'白沙', u'黄金叶', u'利群', u'南京九五', u'黄鹤楼软',  u'黄鹤楼硬', u'娇子', u'钻石荷花', u'玉溪', u'七匹狼尚品',  u'七匹狼软灰']
        return word[word['query'].apply(lambda name:name in tobacco)]['query'].to_dict().values()
    
    result = filter_query(word)
    
    print result

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 16:54:14

    You can try using regular expressions:

    import re
    pattern = re.compile(u'烟|白沙|黄金叶|利群|南京九五|黄鹤楼软|黄鹤楼硬|娇子|钻石荷花|玉溪|七匹狼尚品|七匹狼软灰')
    result = filter(pattern.search, word['query'])

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 16:54:14

    KMP algorithm

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 16:54:14

    KMP
    Manacher
    TireTree

    reply
    0
  • Cancelreply