如何使用商品价格作为词云中的文本频率?
How to use the item price as the text frequency in a wordcloud?
我有以下数据框,需要在python中使用wordcloud进行分析:
Category Price
Dog 500
Cow 1000
Goat 650
import pandas as pd
df = pd.DataFrame({'Category': ['Dog', 'Cow', 'Goat'], 'Price': [500, 1000, 650] })
如何编写代码,使 "Cow" 成为最大文本,然后是 "Goat" 等。我尝试创建 2 个数组:类别和价格,然后将两者相乘并使用结果作为文本。但是,生成的数组以 "DogDogDog..." 和 "Goatgoatgoat" 的形式出现,这不是我想要的。请协助。
您需要添加一个额外的 space,以便单词分开。
import pandas as pd
df = pd.DataFrame({'Category': ['Dog', 'Cow', 'Goat'], 'Price': [3, 4, 5] })
text = ''
for index, row in df.iterrows():
text += (row['Category'] + ' ') * row['Price']
print(text)
输出:
Dog Dog Dog Cow Cow Cow Cow Goat Goat Goat Goat Goat
这是一个更扩展的例子:
import pandas as pd
from matplotlib import pyplot as plt
from wordcloud import WordCloud
df = pd.DataFrame({'Category': ['Apple', 'Pear', 'Lemon', 'Pineapple', 'Nut', 'Banana', 'Grape', 'Orange', 'Plum' ],
'Price': [500, 1000, 650, 800, 3000, 900, 1100, 900, 1400 ]})
text = ''
for row in df.itertuples():
text += (row.Category + ' ') * row.Price
wordcloud = WordCloud(
width=800,height=800,
min_font_size = 10,
background_color='gold',
collocations=False
).generate(text)
# plot the WordCloud image
plt.figure(figsize = (8, 8), facecolor=None)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.tight_layout(pad = 0)
plt.show()
我有以下数据框,需要在python中使用wordcloud进行分析:
Category Price
Dog 500
Cow 1000
Goat 650
import pandas as pd
df = pd.DataFrame({'Category': ['Dog', 'Cow', 'Goat'], 'Price': [500, 1000, 650] })
如何编写代码,使 "Cow" 成为最大文本,然后是 "Goat" 等。我尝试创建 2 个数组:类别和价格,然后将两者相乘并使用结果作为文本。但是,生成的数组以 "DogDogDog..." 和 "Goatgoatgoat" 的形式出现,这不是我想要的。请协助。
您需要添加一个额外的 space,以便单词分开。
import pandas as pd
df = pd.DataFrame({'Category': ['Dog', 'Cow', 'Goat'], 'Price': [3, 4, 5] })
text = ''
for index, row in df.iterrows():
text += (row['Category'] + ' ') * row['Price']
print(text)
输出:
Dog Dog Dog Cow Cow Cow Cow Goat Goat Goat Goat Goat
这是一个更扩展的例子:
import pandas as pd
from matplotlib import pyplot as plt
from wordcloud import WordCloud
df = pd.DataFrame({'Category': ['Apple', 'Pear', 'Lemon', 'Pineapple', 'Nut', 'Banana', 'Grape', 'Orange', 'Plum' ],
'Price': [500, 1000, 650, 800, 3000, 900, 1100, 900, 1400 ]})
text = ''
for row in df.itertuples():
text += (row.Category + ' ') * row.Price
wordcloud = WordCloud(
width=800,height=800,
min_font_size = 10,
background_color='gold',
collocations=False
).generate(text)
# plot the WordCloud image
plt.figure(figsize = (8, 8), facecolor=None)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.tight_layout(pad = 0)
plt.show()