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?

7
  • How did you check the result ? Did you assign the result ? what did you prnt EXACTLY ? Commented Nov 14, 2019 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? Commented Nov 14, 2019 at 21:48
  • Try table.reindex(SORT_WW_reorder, inplace=True) or table = table.reindex(SORT_WW_reorder) please and tell us Commented Nov 14, 2019 at 21:49
  • TypeError: reindex() got an unexpected keyword argument "inplace" and the other suggestion returned all NaN Commented Nov 14, 2019 at 21:51
  • whaat, you just change sort_values by reindex ... the argument I gave is for sort_values Commented Nov 14, 2019 at 21:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

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)
@shaucha Hmm, how about now?
@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.
thanks-- works ! , I just did the assignment part as so: table = table.iloc[pd.Index(SORT_WW_reorder).get_indexer(table.SORT_WW).argsort()]
you did amazing! I will assign this the "answer"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.