Skip to content
Advertisement

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 indices naturally? Answer If you want

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, just pull the

What is the difference between `sorted(list)` vs `list.sort()`?

list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list. When is one preferred over the other? Which is more efficient? By how much? Can a list be reverted to the unsorted state after list.sort() has been performed? Please use Why do these list operations (methods) return

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 ‘value’) I’m

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 by the humanized output (the string), not by the times. Notice that the first value starts with

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 function to which you

Advertisement