在2个数据帧中计数匹配

2024-10-04 11:21:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧,每行包含列表中的文本。这个叫做df

Datum   File    File_type   Text    
Datum                                               
2000-01-27  2000-01-27  0864820040_000127_04.txt    _04     [business, date, jan, heineken, starts, integr..

我还有一个,像这样的

List_type   Words
0   LM_cnstrain.    [abide, abiding, bound, bounded, commit, commi...
1   LM_litigius.    [abovementioned, abrogate, abrogated, abrogate...
2   LM_modal_me.    [can, frequently, generally, likely, often, ou...
3   LM_modal_st.    [always, best, clearly, definitely, definitive...
4   LM_modal_wk.    [almost, apparently, appeared, appearing, appe...

我想在df中创建新的列,在这里应该计算单词的匹配,例如df中有多少单词_lm.文字[0]英寸数据框文本[0]

注意:df有大约500行,df\u lm有6->;所以我需要在df中创建6个新列,以便更新的df看起来像这样

    Datum   ...LM_cnstrain  LM_litigius  Lm_modal_me  ...
2000-01-27  ...   5            3             4
2000-02-25 ...    7            1             0

我希望我的问题是清楚的。 提前谢谢!你知道吗

编辑: 我已经做过smth了。类似的方法是创建一个列表并在其上循环,但是由于df\u lm中的列表非常长,所以这不是一个选项。你知道吗

代码如下所示:

result_list[]
for file in file_list:
    count_growth = 0
    for word in text.split ():
        if word in growth:
            count_growth = count_growth +1
    a={'Grwoth':count_growth}
    result_list.append(a)

Tags: 数据in文本df列表typecountlist
2条回答

根据我的评论,你可以尝试这样的方法:

下面的代码必须在循环中运行,其中第一个df中的文本列必须与下一个df中的所有6个匹配,并使用len(c)中的值生成列

desc = df_lm.iloc[0,1]
matches = df.text.isin(desc)
result = df.text[matches]

如果这对您有帮助,请告诉我,否则将更新/删除答案

因此,我得出以下解决方案:

    for file in file_list:
        count_lm_constraint = 0
        count_lm_litigious = 0
        count_lm_modal_me = 0
          for word in text.split()
        if word in df_lm.iloc[0,1]:
                count_lm_constraint = count_lm_constraint +1 
            if word in df_lm.iloc[1,1]:
                count_lm_litigious = count_lm_litigious +1
            if word in df_lm.iloc[2,1]:
                count_lm_modal_me = count_lm_modal_me +1
            a={"File": name, "Text": text,'lm_uncertain':count_lm_uncertain,'lm_positive':count_lm_positive ....}
result_list.append(a) 

相关问题 更多 >