0

I am trying to draw bar chart with stacked dataframe. I want to change order of num_iter like time_order.

time_order = ["ORIGINAL1", "OMP2","PTHREAD2", "OMP4","PTHREAD4","OMP8","PTHREAD8", "OMP16", "PTHREAD16"]

num_iter    100000000  200000000  400000000
 OMP2        692.5336  1398.5305  2765.7757
 OMP4        362.1932   724.6331  1447.0628
 OMP8        193.0222   382.7540   759.3889
 OMP16       102.5276   214.6385   450.7183
 ORIGINAL1  1360.0577  2731.8207  5440.8003
 PTHREAD2    697.3113  1388.6210  2779.8507
 PTHREAD4    363.9816   721.6508  1432.9843
 PTHREAD8    189.8591   379.8860   764.2684
 PTHREAD16   124.2015   238.9460   478.0660

I used reindex(time_order) -> changed all value nan
I used df.index = time_order -> value remains same spot. df.reset_index(time_order) -> error

How can I change order and have proper bar chart ?

Thanks

jayko03
  • 2,329
  • 7
  • 28
  • 51

2 Answers2

0

Using reindex

df=df.set_index('num_iter').reindex(time_order).reset_index()

df
Out[433]: 
    num_iter  100000000  200000000  400000000
0  ORIGINAL1  1360.0577  2731.8207  5440.8003
1       OMP2   692.5336  1398.5305  2765.7757
2   PTHREAD2   697.3113  1388.6210  2779.8507
3       OMP4   362.1932   724.6331  1447.0628
4   PTHREAD4   363.9816   721.6508  1432.9843
5       OMP8   193.0222   382.7540   759.3889
6   PTHREAD8   189.8591   379.8860   764.2684
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I got error with `num_iter`. To better understand of my dataframe, my original dataframe is, (https://stackoverflow.com/questions/52361577/how-to-make-a-grouped-bar-chart/52361628?noredirect=1#comment91667655_52361628) – jayko03 Sep 17 '18 at 15:59
  • Index([' OMP2', ' OMP4', ' OMP8', ' OMP16', ' ORIGINAL1', ' PTHREAD2',' PTHREAD4', ' PTHREAD8', ' PTHREAD16'], dtype='object') – jayko03 Sep 17 '18 at 16:03
  • @jaykodeveloper then you can just `df=df.reindex(time_order).reset_index()` – BENY Sep 17 '18 at 16:07
  • It gives me all value NaN – jayko03 Sep 17 '18 at 16:10
  • @jaykodeveloper check whether it have space in it – BENY Sep 17 '18 at 16:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180219/discussion-between-jaykodeveloper-and-wen). – jayko03 Sep 17 '18 at 16:26
0

The reindex should work. What does this code give you?

df.reset_index(inplace=True)
order = df['num_iter'].str.extract(r'(\d+)', expand=False).astype(int).sort_values().index
df = df.reindex(order).set_index('num_iter')
Anton vBR
  • 18,287
  • 5
  • 40
  • 46