python - 如何计算标记化数据框中的匹配项
问题描述
我有一个数据框,其中包含 599 个标记化文本,每行一个。我也有这些列表:
grwoth = ['growth', 'grow', 'growing', 'grows']
syergies = ['synergies', 'synergy' ,'accretive', 'accretion','efficiencies' ,'efficient', 'efficiently' ]
intangibles = ['brand','branded','branding','brands','goodwill','patent','patents','goodwil']
customers = ['customer', 'customers' ,'consumer' ,'consumers' ]
technology = ['technological', 'technologically', 'technologies', 'technology', 'innovate', 'innovation', 'innovations', 'innovative', 'innovator', 'innovators']
human = ['employee', 'employees', 'employees', 'team', 'teamed', 'teaming', 'teams', 'Expertise' ]
我想在我的数据框中为每个列表创建一个新列,并计算列表中的单词在每个文本中被计算的频率。
我试图将它们输入到我的原始数据框中(没有标记化),但这也不起作用。我有以下代码:
%%time
growth = ['growth', 'grow', 'growing', 'grows']
synergies = ['synergies', 'synergy' ,'accretive', 'accretion','efficiencies' ,'efficient', 'efficiently' ]
intangibles = ['brand','branded','branding','brands','goodwill','patent','patents','goodwil']
customers = ['customer', 'customers' ,'consumer' ,'consumers' ]
technology = ['technological', 'technologically', 'technologies', 'technology', 'innovate', 'innovation', 'innovations', 'innovative', 'innovator', 'innovators']
human = ['employee', 'employees', 'employees', 'team', 'teamed', 'teaming', 'teams', 'expertise' ]
the = 'Wire'
result_list=[]
count_growth = 0
count_human = 0
count_technology= 0
count_customers = 0
count_intagibles = 0
count_synergies = 0
count_the = 0
for file in file_list:
name = file[len(input_path):]
date = name[11:17]
type_1 = name[17:20]
with open(file, "r", encoding="utf-8", errors="surrogateescape") as rfile:
# We need to encode/decode as some text files are not in utf-8 format
text = rfile.read()
text = text.encode('utf-8', 'ignore')
text = text.decode('utf-8', 'ignore')
for word in text.split():
if word in growth:
count_growth = count_growth +1
if word in synergies:
count_synergies = count_synergies +1
if word in intagibles:
count_intagibles = count_intagibles+1
if word in customers:
count_customers = count_customers +1
if word in technology:
count_technology = count_technology +1
if word in human:
count_human = count_human +1
if word == 'The':
count_the = count_the +1
length = len(text.split())
a={"File": name, "Text": text,'the':count_the, 'Datum': date, 'File_type': type_1, 'length':length, 'grwoth':count_growth, 'synergies': count_synergies,'intagibles':count_intagibles,'customers':count_customers, 'technology':count_technology,'human':count_human,}
result_list.append(a)
这里的问题是,它为每行创建一个总和,而不是为长度创建一个总和。
提前感谢任何解决方案!
解决方案
您只需清除 for 循环内的变量。通过这种方式,它输出各种文件的计数,就像它输出长度一样。
我希望我能正确理解你想要做什么。
下面的代码:
for file in file_list:
count_growth = 0
count_human = 0
count_technology= 0
count_customers = 0
count_intagibles = 0
count_synergies = 0
count_the = 0
name = file[len(input_path):]
date = name[11:17]
type_1 = name[17:20]
with open(file, "r", encoding="utf-8", errors="surrogateescape") as rfile:
# We need to encode/decode as some text files are not in utf-8 format
text = rfile.read()
text = text.encode('utf-8', 'ignore')
text = text.decode('utf-8', 'ignore')
for word in text.split():
if word in growth:
count_growth = count_growth +1
if word in synergies:
count_synergies = count_synergies +1
if word in intagibles:
count_intagibles = count_intagibles+1
if word in customers:
count_customers = count_customers +1
if word in technology:
count_technology = count_technology +1
if word in human:
count_human = count_human +1
if word == 'The':
count_the = count_the +1
length = len(text.split())
a={"File": name, "Text": text,'the':count_the, 'Datum': date, 'File_type': type_1, 'length':length, 'grwoth':count_growth, 'synergies': count_synergies,'intagibles':count_intagibles,'customers':count_customers, 'technology':count_technology,'human':count_human,}
result_list.append(a)
推荐阅读
- xslt - 由 for-each-group 创建的组的 XSLT 索引
- prisma - 在 Prisma 中实现 3 向关系
- scala - 将类导入 Scala 工作表
- python - 将 Holoviews 保存为交互式 html
- r - 当我想使用 R 函数 apply 处理 DocumentTermMatrix 类型的数据集时,出现错误
- javascript - 如何向我的待办事项应用程序添加删除功能?
- python-3.x - python format()结合join和数据转换
- java - 如何使用 Java 中的 SIGAR 或 OSHI API 获取特定应用程序的操作系统进程详细信息?
- .net-core - 升级到核心 3.0,现在我的 Service Fabric 构建失败并出现 FabActUtil 错误
- javascript - 同一对象上的严格等式比较算法