简体   繁体   中英

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: 在此处输入图片说明

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM