0

I want to sort the rows of this dataframe based on the first value of the array in the "boxes" column 0.8299769, 0.04913859 in descending order. How can I do it.

  classes              boxes                                scores
0   7     [0.8299769, 0.27715018, 0.91143125, 0.54477763]   0.999721
2   7     [0.04913859, 0.35264254, 0.15933079, 0.64380944]  0.999397

3 Answers 3

1

you can take the first value of the column you want and create a new column and sort that column then remove it

df['new_col'] = df.boxes.apply(lambda x: x[0])
df=df.sort_values('new_col')
df=df.drop(['new_col'],axis=1)



                                             boxes
1   [0.04913859, 0.35264254, 0.15933079, 0.64380944]
0   [0.8299769, 0.27715018, 0.91143125, 0.54477763]
Sign up to request clarification or add additional context in comments.

Comments

1

try this, create psuedo column sort sort based on it & drop

df = pd.DataFrame({"classes": [7, 2], "boxes": [[0.04913859, 0.35264254, 0.15933079, 0.64380944],
                                                [0.8299769, 0.27715018, 0.91143125, 0.54477763]],
                   "scores": [0.999721, 0.999397]})

df['sort'] = df['boxes'].apply(lambda x: x[0])

df.sort_values(by='sort', ascending=False).drop(columns=['sort'])

   classes                                             boxes    scores
1        2   [0.8299769, 0.27715018, 0.91143125, 0.54477763]  0.999397
0        7  [0.04913859, 0.35264254, 0.15933079, 0.64380944]  0.999721

4 Comments

i tried that but got this error "IndexError: invalid index to scalar variable."
What is the dtype of the column ?
its pandas.core.series.Series type
Unable to reproduce the issue, what does column boxes actual contain? also can u include the sample code for creating the dataframe.
0

You can use the pandas sort_values class, like this:

df.sort_values(by='boxes', ascending=False, inplace=True)

3 Comments

I apologize, I forgot to add the Inplace=True argument so that it changes the original dataframe, I'll edit the answer
I thanks but I get this error. "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" even for that
It worked for me when I tried it on the same dataframe, did you define the variables in the "boxes" column as numpy arrays or python lists?

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.