我有以下每一个单词频率的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
repeat
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