3

Hi I want to sort DataFrame column with given input list values. My list looks like :

inputlist
[309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]

And DataFrame is :

  val    kaywords

195    keyword3
221    keyword5
307    keyword8
309    keyword9
354    keyword0
426    keyword1
585    keyword2
698    keyword4
789    keyword33

Here I want to sort DataFrame column 'val' on basis of given 'inputlist'.

I am expecting following output :

val    kaywords

309    keyword9
585    keyword2
221    keyword5
789    keyword33
195    keyword3
354    keyword0
307    keyword8
698    keyword4
426    keyword1
1
  • @jezrael thanks for quick replay. I have updated my question with expected output. Commented Apr 14, 2019 at 5:38

4 Answers 4

5

Use ordered categorical, but first convert values of list to integers:

inputlist = [309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]

df['val'] = pd.Categorical(df['val'], ordered=True, categories=[int(x) for x in inputlist])
df = df.sort_values('val')
print (df)
   val   kaywords
3  309   keyword9
6  585   keyword2
1  221   keyword5
8  789  keyword33
0  195   keyword3
4  354   keyword0
2  307   keyword8
7  698   keyword4
5  426   keyword1

Another idea if all values from val exist in inputlist:

inputlist = [int(x) for x in inputlist]
df = df.set_index('val').reindex(inputlist).reset_index()
Sign up to request clarification or add additional context in comments.

Comments

0
input_list = [309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]

index = {int(j):i for i,j in enumerate(input_list)}

df = pd.DataFrame({"val":[195, 221, 307, 309, 354, 426, 585, 698, 789]})

df["sort"] = df.apply(lambda row: index[row.val], axis=1)


df = df.sort_values(by=['sort'])

df = df.drop("sort", axis=1)

Comments

0

Maybe the most concise way is to define val as your index and then call it in the order of inputlist:

df.set_index('val').loc[inputlist, :]

Comments

0

Maybe you can create a new data frame

inputlist=[309.0, 585.0, 221.0, 789.0, 195.0, 354.0, 307.0, 698.0, 426.0]

df = pd.DataFrame({"val" :[195.0, 221, 307.0,309.0,354.0,426.0,585.0,698.0,789.0],
                   "keywords" :["keywords3","keywords5","keywords8","keywords9","keywords0","keywords1","keywords2","keywords4","keywords33"]
})

df2=pd.DataFrame()
for i in inputlist:
    df2 = df2.append(df[df["val"]==i])

print(df2)


     val    keywords
3  309.0   keywords9
6  585.0   keywords2
1  221.0   keywords5
8  789.0  keywords33
0  195.0   keywords3
4  354.0   keywords0
2  307.0   keywords8
7  698.0   keywords4
5  426.0   keywords1

Comments

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.