1

I am relatively new to Python, Pandas, and plotting. I am looking to make a custom sort order in a pandas plot using a list, mapping, and sending them through to the plot function.

I am not "solid" on mapping or dict comprehensions. I've looked around a bit on Google and haven't found anything really clear - so any direction to helpful references would be much appreciated.

I have a dataframe that is the result of a groupby:

Exchange
AMEX       267
NYSE      2517
Nasdaq    2747
Name: Symbol, dtype: int64

The numerical column is 'Symbol' and the exchange listing is the index

When I do a straightforward pandas plot

my_plot = Exchange['Symbol'].plot(kind='bar')

I get this:

enter image description here

The columns are in the order of the rows in the dataframe (Amex, NYSE, Nasdaq) but I would like to present, left to right, NYSE, Nasdaq, and Amex. So a "sort" won't work.

There is another post:

Sorting the Order of Bars

that gets at this - but I just couldn't figure it out.

I feel like the solution is one step out of my reach. I think this is a very important concept to get down as it would help me considerably in visualizing data where the not-infrequent case of a custom row presentation in a chart is needed. I'm also hoping discussion here could help me better understand mapping as that seems to be very useful in many instances but I just can't seem to find the right on-line resource to explain it clearly.

Thank you in advance.

Community
  • 1
  • 1
Windstorm1981
  • 2,564
  • 7
  • 29
  • 57

1 Answers1

4

The solution to your problem is putting your output dataframe into desired order:

order = [1,2,0] # the desired order
Exchange['Symbol'].iloc[order]

NYSE      2517
Nasdaq    2747
AMEX       267
Name: Symbol, dtype: int64

As soon as you have the rightly ordered data you can plot it:

Exchange['Symbol'].iloc[order].plot(kind='bar');

enter image description here

Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
  • Thanks for this - so easy and yet it escaped me. The real lesson here for me was the use of iloc. I'm familiar with using it to iterate in two dimensions (e.g., df.iloc[row,column]). I didn't know I could iterate this way where a column is specified. Makes it super simple. Thanks again! – Windstorm1981 Oct 25 '15 at 23:29