如果列B' nodename' (Word)在A列中匹配A' comment_text' (句子)
我有一个数据框有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论