Pandas: Sorting DataFrame List

Using the sort function is pretty easy as of 2017 they are deprecating the old sort function and adding in a new way of writing.

Error: FutureWarning: sort(columns=…) is deprecated, use sort_values(by=…)

Solution:

1
2
>variable_df.sort_values(by=["column_name1", "column_name2"], ascending = [1,0], inplace= True)
>print (variable_df)

Source: Sort Function: Pandas

Ascending: 1 = Ascending & 0 = Descending.

It is an ordered list so the number of items in the list of “by=[]” items must match the quantity of items in the “ascending ” list. In this case we had 2 strings in the list called “column_name1” and “column_name2″,  in ascending  we have”[1,0]”. Which means column_name 1 will be in Ascending order and Column_name2 will be in Descending order.

Here are a list of parameters for this function that will help you maximize it:

Parameters:

by : str or list of str

Name or list of names which refer to the axis items.

axis : {0 or ‘index’, 1 or ‘columns’}, default 0

Axis to direct sorting

ascending : bool or list of bool, default True

Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.

inplace : bool, default False

if True, perform operation in-place

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

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

na_position : {‘first’, ‘last’}, default ‘last’

first puts NaNs at the beginning, last puts NaNs at the end

Leave a Reply

Your email address will not be published. Required fields are marked *