如果列B' nodename' (Word)在A列中匹配A' comment_text' (句子)

发布于 2025-01-26 08:03:09 字数 768 浏览 2 评论 0原文

我有一个数据框有3列。

A列:句子

列B:NodeName

列C:每个节点的度数。

[dataframe列] [1]

我想将B列中存在的每个nodeName与A列中的每个句子匹配,如果该句子中存在该句子,则A列中的句子中存在该句子,则需要将其各自度的总和存储在A数据框中的新列,如摄影。

nodenames = ['canada','government','hope','conservative','harper']
pat = '|'.join(sorted(r"\b{}\b".format(x) for x in nodenames))
finaldfnew['NodeMatch']=finaldfnew["comment_text"].str.findall(pat).apply(", ".join)
finaldfnew

在上面的代码中,我将一些节点名称存储在列表中,并将它们与我的句子列匹配,并将匹配节点存储在新列中。

[例如,节点名称,例如“希望,保守”与第一行句子列相匹配。然后,我将这些匹配节点存储在新的“节点匹配”列中

comment_text    NodeName    Degree  Degree Sum
I am Nauman.           I       2        2+3=5
Cricket is game.   Nauman      3        4
I love Pakistan.   Cricket     4        2

I have a dataframe having 3 columns.

Column A : sentences

Column B : NodeName

Column C : Degrees of each node.

[Dataframe columns][1]

I want to match every Nodename present in column B with every sentence in column A and if that NodeName is present in the sentence in column A then sum of their respective degrees for every row needs to be stored in a new column in the dataframe like DegreeSum.

nodenames = ['canada','government','hope','conservative','harper']
pat = '|'.join(sorted(r"\b{}\b".format(x) for x in nodenames))
finaldfnew['NodeMatch']=finaldfnew["comment_text"].str.findall(pat).apply(", ".join)
finaldfnew

In above code I store some node names in the list and match them with my sentences column and store the matching nodes in new column.

[For example node name like "hope, Conservative" matched with the first row of sentence column. Then i store these matching nodes in new "Node Match" column.][2]

Now I want to add the degrees of matching nodes and sum the average degree into new column like "Degree Sum".

comment_text    NodeName    Degree  Degree Sum
I am Nauman.           I       2        2+3=5
Cricket is game.   Nauman      3        4
I love Pakistan.   Cricket     4        2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文