Skip to content

Tag: sorting

Naturally sorting Pandas DataFrame

I have a pandas DataFrame with indices I want to sort naturally. Natsort doesn’t seem to work. Sorting the indices prior to building the DataFrame doesn’t seem to help because the manipulations I do to the DataFrame seem to mess up the sorting in the process. Any thoughts on how I can resort the i…

Find Nth item in comma separated list in Python

I have a large CSV with comma separated lines of varying length. Sorting another set of data I used split(‘,’) in a loop to separate fields, but this method requires each line to have the same number of entries. Is there a way I can look at a line and, independent of the total number of entries, j…

Sort dictionary by the INT value of the value

There are many posts here about sorting dictionaries in Python so I was careful to read them and hope this is not a duplicate: I’m using a dictionary to hold words as keys and the occurrence of the word as value. This leads to a dictionary that could be like: I want to sort them by occurrence (the &#821…

Sorting a list of unix time stamp values

Given a list of UNIX time stamp values: I need the list to be sorted. When I run this, I got [’31-10-2009 13.45′, ’20-09-2009 14.45′, ’12-12-2009 17.00′, ’07-05-2010 20.00′], which is not at all sorted. Can you see what’s wrong? Answer You’re sorting…

Mergesort with Python

I couldn’t find any working Python 3.3 mergesort algorithm codes, so I made one myself. Is there any way to speed it up? It sorts 20,000 numbers in about 0.3-0.5 seconds Answer You can initialise the whole result list in the top level call to mergesort: Then for the recursive calls you can use a helper …