如何将原始CSV列、Bigram计数和频率导出到新CSV?

2024-04-25 19:34:23 发布

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

这是我第一次使用Python,如果这是一个愚蠢的问题,我深表歉意。我有一个3列的CSV,第一列叫做comment(这是我处理成bigrams的列),第二列叫做comment type,第三列叫做comment date。我很满意这个代码的当前输出,我将第1列(注释)拆分为bigrams,计算频率,并导出到CSV文件中。但现在,我还想将原始csv中的comment type和comment date列(不做任何更改)添加到word和frequency列旁边的导出csv中。我不知道如何去做,并测试了一些想法,但没有工作。你知道吗

import csv
import string
import re
from nltk.util import everygrams
import pandas as pd


from collections import Counter

from itertools import combinations

df = pd.read_csv('modified.csv', 'r', encoding="utf8",
                 names=['comment'])
top_N = 1000
stopwords = nltk.corpus.stopwords.words('english')

RE_stopwords = r'\b(?:{})\b'.format('|'.join(stopwords))

txt = df.comment.str.lower().str.replace(r'\|', ' ').str.cat(sep=' ')

words = nltk.tokenize.word_tokenize(txt)
words = [w for w in words if not w in RE_stopwords]

bigrm = list(nltk.bigrams(words))



word_dist = nltk.FreqDist([' '.join(x) for x in bigrm])
rslt = pd.DataFrame(word_dist.most_common(top_N),
                columns=['Word', 'Frequency'])

print(rslt)
rslt.to_csv('bigram3.csv')

Tags: csvinfromimporttypecommentwordpd
1条回答
网友
1楼 · 发布于 2024-04-25 19:34:23

最后添加的行,在rslt数据帧中创建一个新列,并将数据从原始数据帧复制到这个数据帧。你知道吗

import csv
import string
import re
from nltk.util import everygrams
import pandas as pd


from collections import Counter

from itertools import combinations

df = pd.read_csv('modified.csv', 'r', encoding="utf8",
                 names=['comment'])
top_N = 1000
stopwords = nltk.corpus.stopwords.words('english')

RE_stopwords = r'\b(?:{})\b'.format('|'.join(stopwords))

txt = df.comment.str.lower().str.replace(r'\|', ' ').str.cat(sep=' ')

words = nltk.tokenize.word_tokenize(txt)
words = [w for w in words if not w in RE_stopwords]

bigrm = list(nltk.bigrams(words))



word_dist = nltk.FreqDist([' '.join(x) for x in bigrm])
rslt = pd.DataFrame(word_dist.most_common(top_N),
                columns=['Word', 'Frequency'])
rslt['Column_Type'] = df['comment type']
rslt['Column_Date'] = df['comment date']
print(rslt)
rslt.to_csv('bigram3.csv')

相关问题 更多 >