0

I have a dataset that looks like this, and I need to use the location as the color, the words as the x axis.

country   good   amazing    best
    Aus     12        45      12
   Fiji     25         5      23
    USA     45         5      12
     UK     88       258      18

Ideally, it would look like this:

enter image description here

I tried the following codes:

positive.iplot(kind = 'bar', title = 'Frequency of Positive Words per Country', y = 'Location', x = ['good', 'amazing', 'best'])
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Mtrinidad
  • 157
  • 1
  • 11
  • Does this answer your question? [What is difference between plot and iplot in Pandas?](https://stackoverflow.com/questions/49880314/what-is-difference-between-plot-and-iplot-in-pandas) – Trenton McKinney May 12 '20 at 05:34

1 Answers1

0

To generate a grouped barchart like the one you want, each country and its values should have its own column, so you'll need to transpose your df at at some point.

Also in your example image, the height of the bars doesn't seem to match the values in your df, but I'll assume that's just an image to show the type of barchart you want to create.

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()

enter image description here

Derek O
  • 16,770
  • 4
  • 24
  • 43