我有一个看起来像这样的数据集,我需要将位置用作颜色,将单词用作x轴。
country good amazing best
Aus 12 45 12
Fiji 25 5 23
USA 45 5 12
UK 88 258 18
理想情况下,它看起来像这样:
我尝试了以下代码:
positive.iplot(kind = 'bar', title = 'Frequency of Positive Words per Country', y = 'Location', x = ['good', 'amazing', 'best'])
答案 0 :(得分:0)
要生成所需的分组条形图,每个国家及其值应有其自己的列,因此您需要在某个时候转置df。
同样在您的示例图像中,条形的高度似乎与df中的值不匹配,但是我假设这只是一幅图像,显示要创建的条形图的类型。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'country': ["Aus","Fiji","USA","UK"],
'good': [12,25,45,88],
'amazing': [45,5,5,258],
'best':[12,23,12,18]})
df_values = df[['good','amazing','best']]
df_values.index = df['country']
# transpose the dataframe so that each country and its values have its own column
ax = df_values.T.plot.bar(rot=0)
plt.show()