如何计算统一数据框中的匹配项

时间:2018-11-20 16:43:23

标签: python python-3.x pandas

我有一个数据框,其中包含599个标记化文本,每行一个。我也有这些清单:

grwoth = ['growth', 'grow', 'growing', 'grows']
syergies = ['synergies', 'synergy' ,'accretive', 'accretion','efficiencies' ,'efficient', 'efficiently' ]
intangibles = ['brand','branded','branding','brands','goodwill','patent','patents','goodwil']
customers = ['customer', 'customers' ,'consumer' ,'consumers' ]
technology = ['technological', 'technologically', 'technologies', 'technology', 'innovate', 'innovation', 'innovations', 'innovative', 'innovator', 'innovators']
human = ['employee', 'employees', 'employees', 'team', 'teamed', 'teaming', 'teams', 'Expertise' ]

我想在数据框中为每个列表创建一个新列,并计算每个文本中列表中单词的计数频率。

我试图将它们输入到原始数据框中(没有标记化),但这也不起作用。我有以下代码:

%%time

    growth = ['growth', 'grow', 'growing', 'grows']
    synergies = ['synergies', 'synergy' ,'accretive', 'accretion','efficiencies' ,'efficient', 'efficiently' ]
    intangibles = ['brand','branded','branding','brands','goodwill','patent','patents','goodwil']
    customers = ['customer', 'customers' ,'consumer' ,'consumers' ]
    technology = ['technological', 'technologically', 'technologies', 'technology', 'innovate', 'innovation', 'innovations', 'innovative', 'innovator', 'innovators']
    human = ['employee', 'employees', 'employees', 'team', 'teamed', 'teaming', 'teams', 'expertise' ]
    the = 'Wire'

    result_list=[]
    count_growth = 0
    count_human = 0
    count_technology= 0
    count_customers = 0
count_intagibles = 0
count_synergies = 0
count_the = 0
for file in file_list:
    name = file[len(input_path):]
    date = name[11:17]
    type_1 = name[17:20]
    with open(file, "r", encoding="utf-8", errors="surrogateescape") as rfile:
            # We need to encode/decode as some text files are not in utf-8 format
            text = rfile.read()
            text = text.encode('utf-8', 'ignore')
            text = text.decode('utf-8', 'ignore')

    for word in text.split():
        if word in growth:
            count_growth = count_growth +1
        if word in synergies:
            count_synergies = count_synergies +1 
        if word in intagibles:
            count_intagibles = count_intagibles+1
        if word in customers:
            count_customers = count_customers +1
        if word in technology:
            count_technology = count_technology +1
        if word in human:
            count_human = count_human +1
        if word == 'The':
            count_the = count_the +1
    length = len(text.split())




    a={"File": name, "Text": text,'the':count_the, 'Datum': date, 'File_type': type_1, 'length':length, 'grwoth':count_growth, 'synergies': count_synergies,'intagibles':count_intagibles,'customers':count_customers, 'technology':count_technology,'human':count_human,}
    result_list.append(a)

这里的问题是,它会创建一个总和,但不会像长度那样为每一行求和。

提前感谢您提供任何解决方案!

1 个答案:

答案 0 :(得分:1)

您只需要清除for循环内的变量。这样,它输出各种文件的计数,就像输出长度一样。

我希望我能正确理解你想做什么。

以下代码:

for file in file_list:
    count_growth = 0
    count_human = 0
    count_technology= 0
    count_customers = 0
    count_intagibles = 0
    count_synergies = 0
    count_the = 0
    name = file[len(input_path):]
    date = name[11:17]
    type_1 = name[17:20]
    with open(file, "r", encoding="utf-8", errors="surrogateescape") as rfile:
            # We need to encode/decode as some text files are not in utf-8 format
            text = rfile.read()
            text = text.encode('utf-8', 'ignore')
            text = text.decode('utf-8', 'ignore')

    for word in text.split():
        if word in growth:
            count_growth = count_growth +1
        if word in synergies:
            count_synergies = count_synergies +1 
        if word in intagibles:
            count_intagibles = count_intagibles+1
        if word in customers:
            count_customers = count_customers +1
        if word in technology:
            count_technology = count_technology +1
        if word in human:
            count_human = count_human +1
        if word == 'The':
            count_the = count_the +1
    length = len(text.split())    
    a={"File": name, "Text": text,'the':count_the, 'Datum': date, 'File_type': type_1, 'length':length, 'grwoth':count_growth, 'synergies': count_synergies,'intagibles':count_intagibles,'customers':count_customers, 'technology':count_technology,'human':count_human,}
    result_list.append(a)