gpt4 book ai didi

python - NLTK FreqDist 使用 pandas 到表

转载 作者:行者123 更新时间:2023-12-05 06:16:35 27 4
gpt4 key购买 nike

我有使用 NLTK 得到的频率分布:

[(('ingeniería', 'informática'), 30), (('tecnologías', 'información'), 26), (('sistemas', 'información'), 19), (('big', 'data'), 16), (('ingeniería', 'software'), 14), (('ingeniero', 'técnico'), 11), (('bases', 'datos'), 10), (('información', 'comunicación'), 10), (('tecnologías', 'información', 'comunicación'), 10), (('sistemas', 'operativos'), 9)]

我想将其转换为表格/数据框,最好使用 pandas。

最佳答案

fdist = nltk.FreqDist( ... )    
df_fdist = pd.DataFrame.from_dict(fdist, orient='index')
df_fdist.columns = ['Frequency']
df_fdist.index.name = 'Term'
print(df_fdist)
df_fdist.to_csv(...)

或者:

def cond_freq_dist(data):
""" Takes a list of tuples and returns a conditional frequency distribution as a pandas dataframe. """

cfd = {}
for cond, freq in data:
try:
cfd[cond][freq] += 1
except KeyError:
try:
cfd[cond][freq] = 1
except KeyError:
cfd[cond] = {freq: 1}

return pd.DataFrame(cfd).fillna(0)

关于python - NLTK FreqDist 使用 pandas 到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62054502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com