1
import pandas as pd
import numpy as np 

table = pd.DataFrame()

table["SORT_WW"]= ["03", "50", "01", "52", "03", "48", "02", "47"]
table ["Name"] = ["a", "b", "c", "d", "e", "f", "g", "h"]

And my current table is like:

Current Order

Order I need:

SORT_WW_reorder = pd.Categorical(['45', '44', '46', '47', '48', '49', '50','51', '52', '53', '01', '02', '03', '04', '05', '06', '07'], ordered = True)

What I tried after reading on Stackoverflow answers:

SORT_WW_reorder = pd.Categorical(['45', '44', '46', '47', '48', '49', '50','51', '52', '53', '01', '02', '03', '04', '05', '06', '07'], ordered = True)

table.reindex(SORT_WW_reorder)

It does not do anything once I click on the dataframe "table" on Spyder (the image I shared is same = order is same, did not update). What am I missing?

greencar
  • 315
  • 1
  • 4
  • 18
  • How did you check the result ? Did you assign the result ? what did you prnt EXACTLY ? – azro Nov 14 '19 at 21:44
  • It does not do anything once I click on the dataframe "table" on Spyder (the image I shared is same = order is same, did not update). What am I missing? – greencar Nov 14 '19 at 21:48
  • Try `table.reindex(SORT_WW_reorder, inplace=True)` or `table = table.reindex(SORT_WW_reorder)` please and tell us – azro Nov 14 '19 at 21:49
  • TypeError: reindex() got an unexpected keyword argument "inplace" and the other suggestion returned all NaN – greencar Nov 14 '19 at 21:51
  • whaat, you just change sort_values by reindex ... the argument I gave is for sort_values – azro Nov 14 '19 at 21:51
  • https://stackoverflow.com/questions/50012525/how-to-sort-pandas-dataframe-by-custom-order-on-string-index/50012638 – splash58 Nov 14 '19 at 21:52
  • yes I was inspired by that post, but I still could not figure out the error.. – greencar Nov 14 '19 at 21:53

1 Answers1

0

From my understanding, you want to reorder your rows based on how the values in SORT_WW map to positions in your ordered categorical array.

Here's an option to get the sorted indices by converting your categorical array into an Index:

df.iloc[pd.Index(SORT_WW_reorder).get_indexer(df.SORT_WW).argsort()]                                                                                 

  SORT_WW Name
7      47    h
5      48    f
1      50    b
3      52    d
2      01    c
6      02    g
0      03    a
4      03    e
cs95
  • 379,657
  • 97
  • 704
  • 746
  • this is not the order I want tho.. this is the order I need: SORT_WW_reorder = pd.Categorical(['45', '44', '46', '47', '48', '49', '50','51', '52', '53', '01', '02', '03', '04', '05', '06', '07'], ordered = True) – greencar Nov 14 '19 at 21:56
  • @shaucha Hmm, how about now? – cs95 Nov 14 '19 at 21:58
  • @shaucha If that still isn't what you want, I'm going to stop and ask that you update your question with what the expected output should look like. – cs95 Nov 14 '19 at 22:00
  • thanks-- works ! , I just did the assignment part as so: table = table.iloc[pd.Index(SORT_WW_reorder).get_indexer(table.SORT_WW).argsort()] – greencar Nov 14 '19 at 22:03
  • @shaucha That's implied but that will do the job, yes. – cs95 Nov 14 '19 at 22:03
  • how else were you thinking to apply it to the dataframe? if you dont do the assignment it was still showing the old order – greencar Nov 14 '19 at 22:04
  • @shaucha Sorry, guess I wasn't clear. My style of answering questions is I usually don't assign the output back (that I leave to the OP to figure out). Rather than doing `result = x; print(result)` I find it cleaner to just write `x` and show the output below, this is literally IPython session dump without the prompts. – cs95 Nov 14 '19 at 22:06
  • 1
    you did amazing! I will assign this the "answer" – greencar Nov 14 '19 at 22:07