Home > Enterprise >  seaborn barplot to compare values from a dataframe with two rows
seaborn barplot to compare values from a dataframe with two rows

Time:09-29

I have a dataFrame that looks kind of like this.

      - X_1     - X_2     - X3      - ....
0     - float64 - float64 - float64 - ...
1     - ...

Example: df=pd.DataFrame({"X_1": [4, 2], "X_2": [2, 3], "X_3": [1, 1]})

In my case, there are two rows and a few dozen columns. I'm trying to make a barplot that shows the values at index 0 and 1 side-by-side, from greatest to smallest, sorted on Index 0. e.g.,

      |
      |  0
value |  0         1
      |  0 1     0 1
      |  0 1     0 1     0 1
      _______________________
         X_1     X_2     X_3

When I try to run the command below, I get a couple different errors.

I run this: order=df.sort_values(df.iloc[0], ascending=False).df.columns

And get this: AttributeError: 'list' object has no attribute 'sort_values

If i just ignor ordering, I try this: sns.barplot(df, x=df.columns, y=df.values)

I get this: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I'm suspecting that I need to re-shape my dataFrame so it is friendier. I think the shape that might work is somethinging like below.

Category - Y_1     - Y2
X_1      - float64 - float64
X_2      - float64 - float64
X_3      - float64 - float64
...

Then, I think I could successfully do my ordering and plotting like blow.

Order. order=df.sort_values(Y_1, ascending=False).Category Plot. sns.barplot(df, x=df.Category, y=df.columns~'Category')

My question is: How do I get the desired plot (illustrated above) from my dataframe? I outlined something that I think my lead there but this could be completely ill guided. Please let me know if you have any ideas. Thnank you!

CodePudding user response:

You can do:

df.T.plot.bar()

This will give you (using the df defined in your question):

enter image description here

  • Related