我想对数据帧中的文本进行分类。我使用字典检查值是否在词干文本列中,然后在同一列中应用过滤器,在新列中分配类别。
过滤器是:如果至少33%的值是True print 1,否则打印0。
注意:字典中的键代表类别。
我检查了第一行的类型:它是一个列表,但当我应用其他方法时,它不起作用。所以我只将其应用于第一行,但我不知道如何准确地传输到所有其他行。
dictionary = {'cat_1' : ['some', stemming', 'bunch'], 'cat_2' : ['to', 'so'], 'cat_3': ['stemming', 'words', 'many', 'bunch']}
dataframe = {'Articles' : ['article1', 'article2', 'article3', 'article4'], 'Text' : [['some', 'stemming', 'words'], ['to' , 'much', 'stemming', 'words'], ['another', 'bunch', 'of', 'stemming', 'words'], ['so', 'many', 'stemming', 'words']]}
test = dataframe.text[0]
for item in dictionary.values():
filt = []
for i in item:
if i in test:
filt.append(True)
else:
filt.append(False)
print(filt)
umbral = len(filt) * 0.33
Trues = filt.count(True)
if Trues > umbral:
print('1')
else:
print('0')输出为:
[True, True, False]
1
[True, False]
1
[True, True, False, True]
1 我想将其应用于列'text‘的每一行,并使用1或/和0为每个结果设置一个列。例如:在第一行,它将是:
|----------|-------|-------|-------|
| Articles | cat_1 | cat_2 | cat_3 |
|----------|-------|-------|-------|
| article1 | 1 | 1 | 0 |
|----------|-------|-------|-------|
| article2 | 0 | 1 | 1 |
|----------|-------|-------|-------|
| article3 | 1 | 0 | 0 |
|----------|-------|-------|-------|发布于 2019-08-16 15:57:10
你能不能不使用:
def cat(z):
return [True if z[i] in d.values() else False for i in range(0,len(z))]
dataframe['test'].map(lambda x: cat(x))其中df表示您的dataframe.text
https://stackoverflow.com/questions/57520365
复制相似问题