我试图在标记化数据帧的每一行中查找最常见的单词,如下所示:
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) )
但它报告的清单是:
^{pr2}$我想创建以下数据帧代替
print(final_df)
KeyWords
revenue, apple, inc., aapl, reported
...
注意:最终数据帧的行不是列表,而是单个文本值,例如revenue,apple,inc.,aapl,reported,not,[revenue,apple,inc.,aapl,reported]
像这样的事?使用^{}
输出:
^{pr2}$使用} &;^{}
for
循环^{输出:
不确定是否可以更改返回格式,但可以使用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]
使用
df.apply
例如:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐