【问题标题】:Most Frequent words in each row每行中出现频率最高的词
【发布时间】:2019-05-02 13:58:26
【问题描述】:

我正在尝试在标记化 Dataframe 的每一行中找到最常用的单词,如下所示:

print(df.tokenized_sents)

['apple', 'inc.', 'aapl', 'reported', 'fourth', 'consecutive', 'quarter', 'record', 'revenue', 'profit', 'combination', 'higher', 'iphone', 'prices', 'strong', 'app-store', 'sales', 'propelled', 'technology', 'giant', 'best', 'year', 'ever', 'revenue', 'three', 'months', 'ended', 'sept.']

['brussels', 'apple', 'inc.', 'aapl', '-.', 'chief', 'executive', 'tim', 'cook', 'issued', 'tech', 'giants', 'strongest', 'call', 'yet', 'u.s.-wide', 'data-protection', 'regulation', 'saying', 'individuals', 'personal', 'information', 'been', 'weaponized', 'mr.', 'cooks', 'call', 'came', 'sharply', 'worded', 'speech', 'before', 'p…']

...

wrds = []
for i in range(0, len(df) ):

    wrds.append( Counter(df["tokenized_sents"][i]).most_common(5) )

但它会报告一个列表:

print(wrds)

[('revenue', 2), ('apple', 1), ('inc.', 1), ('aapl', 1), ('reported', 1)]
...

我想创建以下数据框;

print(final_df)

KeyWords                                                                         
revenue, apple, inc., aapl, reported
...

注意最终数据框的行不是列表,而是单个文本值,例如收入,苹果公司,苹果公司,报告,,[收入,苹果公司,苹果公司,报告]

【问题讨论】:

  • 你能给我们输入数据框吗?
  • 是开头打印的列(df.tokenized_sents),其他列与此无关;

标签: python pandas nlp


【解决方案1】:

这样的?使用 .apply()

# creating the dataframe
df = pd.DataFrame({"token": [['apple', 'inc.', 'aapl', 'reported', 'fourth', 'consecutive', 'quarter', 'record', 'revenue', 'profit', 'combination', 'higher', 'iphone', 'prices', 'strong', 'app-store', 'sales', 'propelled', 'technology', 'giant', 'best', 'year', 'ever', 'revenue', 'three', 'months', 'ended', 'sept.'], ['brussels', 'apple', 'inc.', 'aapl', '-.', 'chief', 'executive', 'tim', 'cook', 'issued', 'tech', 'giants', 'strongest', 'call', 'yet', 'u.s.-wide', 'data-protection', 'regulation', 'saying', 'individuals', 'personal', 'information', 'been', 'weaponized', 'mr.', 'cooks', 'call', 'came', 'sharply', 'worded', 'speech', 'before', 'p…']
]})
# fetching 5 most common words using .apply and assigning it to keywords column in dataframe
df["keywords"] = df.token.apply(lambda x: ', '.join(i[0] for i in Counter(x).most_common(5)))
df

输出:

    token   keywords
0   [apple, inc., aapl, reported, fourth, consecut...   revenue, apple, inc., aapl, reported
1   [brussels, apple, inc., aapl, -., chief, execu...   call, brussels, apple, inc., aapl

使用for循环 .loc() & .itertuples()

df = pd.DataFrame({"token": [['apple', 'inc.', 'aapl', 'reported', 'fourth', 'consecutive', 'quarter', 'record', 'revenue', 'profit', 'combination', 'higher', 'iphone', 'prices', 'strong', 'app-store', 'sales', 'propelled', 'technology', 'giant', 'best', 'year', 'ever', 'revenue', 'three', 'months', 'ended', 'sept.'], ['brussels', 'apple', 'inc.', 'aapl', '-.', 'chief', 'executive', 'tim', 'cook', 'issued', 'tech', 'giants', 'strongest', 'call', 'yet', 'u.s.-wide', 'data-protection', 'regulation', 'saying', 'individuals', 'personal', 'information', 'been', 'weaponized', 'mr.', 'cooks', 'call', 'came', 'sharply', 'worded', 'speech', 'before', 'p…']
]})
df["Keyword"] = ""
for row in df.itertuples():
    xount = [i[0] for i in Counter(row.token).most_common(5)]
    df.loc[row.Index, "Keyword"] = ', '.join(i for i in xount)
df

输出:

    token   Keyword
0   [apple, inc., aapl, reported, fourth, consecut...   revenue, apple, inc., aapl, reported
1   [brussels, apple, inc., aapl, -., chief, execu...   call, brussels, apple, inc., aapl

【讨论】:

    【解决方案2】:

    使用df.apply

    例如:

    import pandas as pd
    from collections import Counter
    tokenized_sents = [['apple', 'inc.', 'aapl', 'reported', 'fourth', 'consecutive', 'quarter', 'record', 'revenue', 'profit', 'combination', 'higher', 'iphone', 'prices', 'strong', 'app-store', 'sales', 'propelled', 'technology', 'giant', 'best', 'year', 'ever', 'revenue', 'three', 'months', 'ended', 'sept.'], 
                       ['brussels', 'apple', 'inc.', 'aapl', '-.', 'chief', 'executive', 'tim', 'cook', 'issued', 'tech', 'giants', 'strongest', 'call', 'yet', 'u.s.-wide', 'data-protection', 'regulation', 'saying', 'individuals', 'personal', 'information', 'been', 'weaponized', 'mr.', 'cooks', 'call', 'came', 'sharply', 'worded', 'speech', 'before', 'p…']
    
    ]
    
    df = pd.DataFrame({"tokenized_sents": tokenized_sents})
    final_df = pd.DataFrame({"KeyWords" : df["tokenized_sents"].apply(lambda x: [k for k, v in Counter(x).most_common(5)])}) 
    #or
    #final_df = pd.DataFrame({"KeyWords" : df["tokenized_sents"].apply(lambda x: ", ".join(k for k, v in Counter(x).most_common(5)))})
    print(final_df)
    

    输出:

                                   KeyWords
    0  [revenue, apple, aapl, sales, ended]
    1   [call, saying, apple, issued, aapl]
    

    【讨论】:

    • 是否可以不以列表的形式,而是以单个字符串的形式:revenue、apple、aapl、(...)?我尝试使用 final_df.KeyWords.map(lambda x: x.lstrip('[]')) 但它不起作用@Rakesh
    • final_df = pd.DataFrame({"KeyWords" : df["tokenized_sents"].apply(lambda x: ", ".join(k for k, v in Counter(x).most_common(5)))})
    • 完美,请将其添加到答案中,我会立即接受;再次感谢!
    【解决方案3】:

    不确定是否可以更改返回格式,但可以使用 apply 和 lambda 重新格式化列。例如。 df = pd.DataFrame({'wrds':[[('revenue', 2), ('apple', 1), ('inc.', 1), ('aapl', 1), ('reported', 1)]]})

    df.wrds.apply(lambda x: [item[0] for item in x])

    只返回单词列表[revenue, apple, inc., aapl, reported]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多