gpt4 book ai didi

python - 如何将单词计数转换为 Python 中的实际单词列表

转载 作者:行者123 更新时间:2023-11-28 20:13:58 25 4
gpt4 key购买 nike

我有以下 Pandas 数据框,其中包含每个单词的频率:

d = {'Count' : pd.Series([2, 3, 5]), 'Term': pd.Series(['Cool', 'New', 'Very'])}
df = pd.DataFrame(d)

表格(打印 df):

  Count     Term  
0 2 Cool
1 3 New
2 5 Very

但是,我想将每个单词的字面频率转换为一个名为“Term”的列中每个相应频率的单词列表:

输出看起来像这样:

c = {'Term': pd.Series(['Cool', 'Cool', 'New','New','New','Very', 'Very','Very','Very','Very'])}
df_2 = pd.DataFrame(c)
print df_2


Term
0 Cool
1 Cool
2 New
3 New
4 New
5 Very
6 Very
7 Very
8 Very
9 Very

有人可以帮助我构建可扩展的东西来实现这一目标吗?

最佳答案

IIUC pandas 重复

df.Term.repeat(df.Count)
Out[350]:
0 Cool
0 Cool
1 New
1 New
1 New
2 Very
2 Very
2 Very
2 Very
2 Very
Name: Term, dtype: object

关于python - 如何将单词计数转换为 Python 中的实际单词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51751420/

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