python - 类似于 "Generate n-grams from Pandas column while persisting another column"(未解决),但有值

标签 python pandas n-gram

我有一些文本行,然后是它们的相关权重。

Weight, Text
10, "I like apples"
20, "Someone needs apples"

是否可以获取组合,并将值保留在权重列中?像这样的东西:

weight, combinations
10, [I like]
10, [I apples]
10, [like apples]
20, [someone needs]
20, [someone apples]
20, [needs apples]

“从 Pandas 列生成 n-grams,同时保留另一列”(未解决)是一个类似的问题,但尚未解决。

谢谢!!!

最佳答案

from itertools import combinations
import pandas as pd

df = pd.DataFrame({'Weight': [10, 20],
                   'Text': ["I like apples", "Someone needs apples"]})
df['Combinations'] = df.Text.apply(lambda x : list(combinations(x.split(), 2)))
df = df.explode('Combinations')
df.drop('Text', axis=1, inplace=True)

print(df)

输出:

   Weight       Combinations
0      10          (I, like)
0      10        (I, apples)
0      10     (like, apples)
1      20   (Someone, needs)
1      20  (Someone, apples)
1      20    (needs, apples)

关于python - 类似于 "Generate n-grams from Pandas column while persisting another column"(未解决),但有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65971168/

相关文章:

python - 使用 django 模板将 json 填充为 html

python - 计算每个月的支出金额,这取决于另一个列值 ID

lucene - SOLR:NGramFilterFactory 的问题

r - NGramTokenizer 未按预期工作

elasticsearch - 查询elasticsearch以使所有分析的ngram token 都匹配

python - 如何将 PIL(或任何模块)安装到树莓派上?

python - 为什么 QPrinterDialog 不能在 PySide 中正确采用每台打印机的默认选项?

python - 如何删除 dask 数据框中带有 nan 单元格的行?

python - 将 Excel 文件读取到 pandas 数据框的更快方法

python - 使用 __set__ 获得类级别类型描述符的方法