找出每个目标类别中每个单词在句子中出现的次数

2024-09-24 02:19:30 发布

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

我有这样的东西。你知道吗

Sentence                                        Target
We regret to inform you about the result.        1
We are glad to inform you about the result.      2
We would like to inform you about the result.   3
We are surprised to see the result.              4

我想要一个像这样的字数

Word    Target 1    Target 2    Target 2    Target 4
Result     1           1            1           1
Inform     1           1            1           0
Surprised   0           0           0           1

。。。等等。我该怎么做?你知道吗


Tags: thetoyoutargetresultaresentencelike
1条回答
网友
1楼 · 发布于 2024-09-24 02:19:30

你需要

  1. 删除标点和小写的数据
  2. 按空格拆分
  3. stack创建序列
  4. groupbyTarget
  5. 找到每个目标词的value_counts
  6. unstack所需输出的结果

df.Sentence.str.replace('[^\w\s]', '')\
  .str.lower()\
  .str.split(expand=True)\
  .set_index(df.Target)\
  .stack()\
  .groupby(level=0)\
  .value_counts()\
  .unstack(0, fill_value=0)\
  .add_prefix('Target ')


Target     Target 1  Target 2  Target 3  Target 4
about             1         1         1         0
are               0         1         0         1
glad              0         1         0         0
inform            1         1         1         0
like              0         0         1         0
regret            1         0         0         0
result            1         1         1         1
see               0         0         0         1
surprised         0         0         0         1
the               1         1         1         1
to                1         1         1         1
we                1         1         1         1
would             0         0         1         0
you               1         1         1         0

相关问题 更多 >