【问题标题】:Counting the frequency of words in a pandas column and counting another column计算熊猫列中单词的频率并计算另一列
【发布时间】:2022-01-18 00:12:03
【问题描述】:

我有一个带有 cmets 及其标签的数据框。

Comments Label
I love my Teammates Positive
We need higher pay Suggestions
I hate my boss Negative

我想得到类似的输出

Word count Positive Negative Suggestions
I 2 1 1 0
my 2 1 1 0
Teammates 1 1 0 0
love 1 1 0 0
We 1 0 0 1
need 1 0 0 1
higher 1 0 0 1
pay 1 0 0 1
hate 1 0 1 0
boss 1 0 1 0

我可以通过使用来获得字数

df.Comments.str.split(expand=True).stack().value_counts() 

但我无法获得标签计数。任何帮助将不胜感激!

【问题讨论】:

    标签: python python-3.x pandas dataframe pivot


    【解决方案1】:

    您可以执行以下操作

    out = (
        df.assign(Word=lambda df: df.Comments.str.split())   # Create a column 'World' with the list of words 
          .explode('Word')  # explode the list of words into new rows 
          .pipe(lambda df: pd.crosstab(df.Word, df.Label)) # cross table/ pivot table between 'Word' and 'Label' columns
          .assign(Count=lambda df: df.sum(axis=1))   # Count the column's total
          .reset_index()  # 'Word' index to column
          .rename_axis(columns=None) # remove the name ('Label') of the columns axis
    )
    

    输出:

    >>> out 
    
            Word  Negative  Positive  Suggestions  Count
    0          I         1         1            0      2
    1  Teammates         0         1            0      1
    2         We         0         0            1      1
    3       boss         1         0            0      1
    4       hate         1         0            0      1
    5     higher         0         0            1      1
    6       love         0         1            0      1
    7         my         1         1            0      2
    8       need         0         0            1      1
    9        pay         0         0            1      1
    

    【讨论】:

    • 干净且不言自明!
    • @YanniTheDeveloper 谢谢 ;)
    • 谢谢你们!
    【解决方案2】:

    你可以使用:

    out = (
        df['Comments'].str.split().explode().to_frame('Word').join(df['Label']).assign(value=1) \
                      .pivot_table('value', 'Word', 'Label', aggfunc='count', fill_value=0) \
                      .assign(Count=lambda x: x.sum(axis=1))
    )
    

    输出:

    >>> out
    Label      Negative  Positive  Suggestions  Count
    Word                                             
    I                 1         1            0      2
    Teammates         0         1            0      1
    We                0         0            1      1
    boss              1         0            0      1
    hate              1         0            0      1
    higher            0         0            1      1
    love              0         1            0      1
    my                1         1            0      2
    need              0         0            1      1
    pay               0         0            1      1
    

    详情:

    第 1 步。 将每个评论分解为单词并分配一个虚拟值。

    out = df['Comments'].str.split().explode().to_frame('Word').join(df['Label']).assign(value=1)
    print(out)
    
    # Output:
            Word        Label  value
    0          I     Positive      1
    0       love     Positive      1
    0         my     Positive      1
    0  Teammates     Positive      1
    1         We  Suggestions      1
    1       need  Suggestions      1
    1     higher  Suggestions      1
    1        pay  Suggestions      1
    2          I     Negative      1
    2       hate     Negative      1
    2         my     Negative      1
    2       boss     Negative      1
    

    第 2 步。 旋转您的数据框。

    out = out.pivot_table('value', 'Word', 'Label', aggfunc='count', fill_value=0)
    print(out)
    
    # Output:
    Label      Negative  Positive  Suggestions
    Word                                      
    I                 1         1            0
    Teammates         0         1            0
    We                0         0            1
    boss              1         0            0
    hate              1         0            0
    higher            0         0            1
    love              0         1            0
    my                1         1            0
    need              0         0            1
    pay               0         0            1
    

    第 3 步。:创建 count 列。

    out = out.assign(Count=lambda x: x.sum(axis=1))
    print(out)
    
    # Output:
    Label      Negative  Positive  Suggestions  Count
    Word                                             
    I                 1         1            0      2
    Teammates         0         1            0      1
    We                0         0            1      1
    boss              1         0            0      1
    hate              1         0            0      1
    higher            0         0            1      1
    love              0         1            0      1
    my                1         1            0      2
    need              0         0            1      1
    pay               0         0            1      1
    

    【讨论】:

    • 谢谢!是的,有没有办法可以在单独的列中添加与每个单词相关的原始评论?
    • 如何将原始数据框与派生数据框合并?我需要 cmets 以及单词和计数吗?
    • 对于I和my这两个词,有2个cmets包含这个词。解释一下你想如何设置新列?通过', ' 加入 cmets 或创建 cmets 列表?
    • 谢谢科拉连!我找到了一种解决方法,直接使用 Power BI 来实现这一点,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2013-12-28
    • 2021-03-17
    • 2021-12-26
    • 2019-11-27
    相关资源
    最近更新 更多