我有一个数据框架a,其中包含docid(文档ID)、title(文章标题)、lineid(行ID,也称为段落位置)、text和tokencount(包括空格在内的单词计数):
docid title lineid text tokencount
0 0 A 0 shopping and orders have become more com... 66
1 0 A 1 people wrote to the postal service online... 67
2 0 A 2 text updates really from the U.S. Postal... 43
...
我想基于包含title
、lineid
、count
和query
的数据帧创建一个新的数据帧
query
是包含一个或多个单词的文本字符串,如“数据分析”、“文本消息”或“购物和订单”
count
是query
中每个单词的计数
新的数据帧应如下所示:
title lemma count lineid
A "data" 0 0
A "data" 1 1
A "data" 4 2
A "shop" 2 0
A "shop" 1 1
A "shop" 2 2
B "data" 4 0
B "data" 0 1
B "data" 2 2
B "shop" 9 0
B "shop" 3 1
B "shop" 1 2
...
如何制作一个函数来生成这个新的数据帧
我已经从具有列count
的创建了一个新的数据帧df
df = A[['title','lineid']]
df['count'] = 0
df.set_index(['title','lineid'], inplace=True)
此外,我还创建了一个计算查询单词数的函数
from collections import Counter
def occurrence_counter(target_string, query):
data = dict(Counter(target_string.split()))
count = 0
for key in query:
if key in data:
count += data[key]
return count
但是,如何使用它们来生成新数据帧的函数呢
如果我理解正确,您可以使用内置的pandas函数执行此操作:^{} 来计算} 以重塑为最终的柱结构
queries
^{给定样本
df
:第一个{a3}这个{}:
然后^{} 进入最终的列结构:
这将处理您的场景:
相关问题 更多 >
编程相关推荐