从python语句中提取单词

2024-06-12 09:41:08 发布

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

我有一个文本/csv文件格式的数据集。它有两列如下=

ID - TEXT
1 - this probability is 10-15% 
2 - approximately 20% probablity 
3 - 15% probability 

我试图使用NLTK从存在关键字'Probability'的数据中提取数字。在

这就是我的代码。在

^{pr2}$

我希望输出看起来像这样-

ID - Value 
1 - 10
2 - 20
3 - 15

如果有人能朝着正确的方向努力,那将是非常有帮助的。在


Tags: csv数据代码text文本idis数字
1条回答
网友
1楼 · 发布于 2024-06-12 09:41:08

这段代码应该行得通,或者至少能帮你解决它 这是完整的代码

import csv
import nltk
impor re
import pandas as pd
from nltk import sent_tokenize, word_tokenize

tweet = []

data_file = pd.read_excel(r'data_excel.xlsx',sheet_name = 'data')
df = pd.DataFrame(data_file, columns = ['ID','TEXT'])


cols = ['ID', 'Num']
newDataFrame = pd.DataFrame(columns=cols)


#this should provide you with a list of both ID and txt
ID = df.iloc[:,0].values
TEXT  = df.iloc[:,1].values


#loop throug the id and set occurence of the number of probability
for i in range(1, len(ID)):
    number_list = re.findall(r'\b\d+\b', TEXT[i])

    newDataFrame.iloc[i].ID = ID
    newDataFrame.iloc[i].Num = number_list

print(newDataFrame)

相关问题 更多 >