theo123490
theo123490

Reputation: 58

Pandas Datframe sort_values on binary data

I am new to pandas, I have a dataframe that I would sort with binary data so that the data is separated between the 0's and 1's. I am using df.sort_values, will df.sort_values sort the data the same way every time?

EDIT: here is an example of the data frame that I use: enter image description here

I would like to sort from the data frame from the second column.

here is the code I use:

sort_data = df.sort_values(['Diagnosis'],axis=0,ascending=True)

Upvotes: 0

Views: 393

Answers (1)

rafaelc
rafaelc

Reputation: 59274

The quick answerer is: it depends on your sorting algorithm.

There exists stable and unstable sorting algorithms. sort_values supports merge sort, which is stable, but has a default algorithm (quick sort) that is unstable. A stable sorting algorithm keeps the output of elements with same key in their same order as they were in the input.

From the docs,

kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’

Choice of sorting algorithm. See also ndarray.np.sort for more information. mergesort is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label.

Therefore, if you pick a stable algorithm, it will sort the data in the same way every time. It is good to know, however, the limitations of the sorting algorithms you use. There some nice sources available online.

Upvotes: 2

Related Questions