I have a large CSV file, which is a log of caller data. A short snippet of my file: I want to sort the entire list by the frequency of occurrence of customers so it will be like: I’ve tried groupby, but that only prints out the Company Name and the frequency but not the other columns, I also tried
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
Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?
What’s the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data? Question: Will an OrderedDict preserve the order of a list of tuples, or tuple of tuples or tuple of lists or list of lists etc. passed at the time of initialization (2nd & 3rd example above)? How does one go about
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
Order a list of numbers without built-in sort, min, max function
If I have a list that varies in length each time and I want to sort it from lowest to highest, how would I do that? If I have: [-5, -23, 5, 0, 23, -6, 23, 67] I want: [-23, -6, -5, 0, 5, 23, 23, 67] I start with this: BUT this only goes through once and I get:
Rank items in an array using Python/NumPy, without sorting array twice
I have an array of numbers and I’d like to create another array that represents the rank of each item in the first array. I’m using Python and NumPy. For example: Here’s the best method I’ve come up with: Are there any better/faster methods that avoid sorting the array twice? Answer Use advanced indexing on the left-hand side in the