I have a dataframe that looks like this: Col1 Col2 Bonnie Anna Connor Ethan Sophia Daniel And I want to sort its content alphabetically so that the final result is: Col1 Col2 Anna Bonnie Connor Ethan Daniel Sophia I want each pair to be ordered alphabetically. As they are in different columns, I don’t know how to sort them directly

# Tag: sorting

## How to group similar numbers with ranges/conditions and merge IDs using dataframes?

Please, I have a dataframe that is listed in ascending order. My goal is to average similar numbers (numbers that are within 10% of each other in ‘both directions’) and concate their ‘Bell’ name together. For example, the image shows the input and output dataframe. I tried coding it but I stuck on how to progress. Answer Assuming you really

## Sort the digits of a 1GB file containing a single number efficiently

I’m trying to print in ascending order a 1GB file containing a randomly generated big number. This is the code that I’m using to generate the random number for my test (found it here). The following python code works OK and takes a bit less than 4 minutes. But I was told this can be accomplished in about 15 seconds

## How to reorder pandas dataframe based off list containing column order

Say I have a dataframe ‘df’ that contains a list of files and their contents: How can I reorder this df if I have ordered lists of how the ‘Field’ column should be ordered? So that the resulting df is re ordered like so (I am not trying to just sort ‘Field’ in reverse alphabetical order, this example is just

## Sort a number by its digits

I have to sort a vector of integers (all integers have the same length). Integers with the same first digit must be sorted in relation to the second digits, and numbers with the same: first and second digits are sorted by third digit etc. Also, the subsequent digits are sorted alternately (once ascending and once descending) So when I have

## How to delete values in a dict based on the presence of child values

I have a dict that looks like this: How can I iterate over the elements of this dict, and exclude any element that has “‘parent’ : None” and no child element (meaning, no other element in this dict has this one as a parent). Expected output: Answer I think this function can help you To use it:

## Trouble sorting list of lists in Python with Insertion Sort

I am currently making a Python program which takes in the name of a grocery item, the aisle it’s on, its location along the aisle, and the price. It holds the data in a list of lists, with each inner list containing 4 elements. The program attempts to create the optimal route through the aisles, with traversal on odd aisles

## (python) quicksort working for ordered data, but not for unordered data

I am working on an implementation of recursive quicksort in python. I am working with very large data sets (10,000 – 1,000,000 elements). When feeding it ordered data (i.e. changing an array sorted from largest -> smallest to smallest -> largest) it works fine. But when giving it unordered data, it doesn’t seem to work at all. I’m using a

## Ordering a two-dimensional array relative to the main diagonal

Given a two-dimensional array T of size NxN, filled with various natural numbers (They do not have to be sorted in any way as in the example below.). My task is to write a program that transforms the array in such a way that all elements lying above the main diagonal are larger than each element lying on the diagonal

## Sort by custom function in R

In python, I can do something like It gives me [1, 5, 99, 100, -5, -7, -100] It is sorted by positive/negative number and abs value. How can I do the same thing in R? Without splitting into positive and negative numbers? a = c(1,100,5,-5,-7,99,-100) Answer Use the order() function: Created on 2022-03-22 by the reprex package (v2.0.1) Another possibility