sorting

how to sort only some of the columns in a data frame in pandas?

☆樱花仙子☆ 提交于 2020-01-23 12:25:12
问题 Is there a way to sort only some elements of a list in a user-defined manner? import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(5, 6), columns=['x','a','c','y','b','z']) I'd like to sort the columns of df in a way that the first 3 columns are [x, y, z] (in this order), and it doesn't matter where the remaining columns are placed. For this example I could do it manually, but as the list gets bigger it would be convenient to use a more appropriate method. I thought of

Ranking Contest Results of Images with 5-Star Ratings

老子叫甜甜 提交于 2020-01-23 09:29:07
问题 I run a calendar photo contest that uses a 5-star rating system which ranks the images according to their average rating. However, I would like to factor in the total number of votes a photo receives to get a more accurate ranking. For example, I do not want an image with 1 5-star vote (Avg rating: 5) getting ranked above an image with 10 5-star votes and 1 4-star vote (Avg rating: 4.9). I know this topic has been raised before, but I can't seem to find a straightforward answer to apply to my

Why does array_unique sort the values?

爱⌒轻易说出口 提交于 2020-01-23 05:23:08
问题 This refers to one of my previous questions: array_unique vs array_flip - This states that array_flip(array_flip()) is much quicker than array_unique() when dealing with simple strings and integers. What I would like to know is why array_unique() creates a copy of the array, sorts it then removed the duplicates The source for both functions is available here. Thanks in advance! 回答1: If you think about it algorithmically, the way to remove duplicates is to go through a list, keep track of

Sorting a dict with tuples as values

十年热恋 提交于 2020-01-23 01:38:11
问题 I have a dictionary that looks like this: {'key_info': (rank, raw_data1, raw_data2), 'key_info2': ...} Basically I need back a list of the keys in sorted order, that is sorted based on the rank field in the tuple. My code looks something like this right now ( diffs is the name of the dict above): def _sortRanked(self): print(type(self.diffs)) return sorted(self.diffs.keys(), key=lambda x: x[1], reverse=True) that right now returns this when I run it: return sorted(self.diffs.keys(), key

sort 2d numpy array lexicographically

我是研究僧i 提交于 2020-01-22 20:54:06
问题 I have a large 2d array with hundreds of columns. I would like to sort it lexicographically, i.e. by first column, then by second column, and so on until the last column. I imagine this should be easy to do but I haven't been able to find a quick way to do this. 回答1: This is what numpy.lexsort is for, but the interface is awkward. Pass it a 2D array, and it will argsort the columns , sorting by the last row first, then the second-to-last row, continuing up to the first row: >>> x array([[0, 0

Java: Sorting an array based on another array with indexOf method

时光毁灭记忆、已成空白 提交于 2020-01-22 19:42:06
问题 I want to iterate through two arrays(A, B) based on the sorted order of another array(indexes), which is 10, 34, 32, 21 in this case. String[] A: a, b, c, d String[] B: e, f, g, h int[] indexes: 10, 34, 32, 21 Apology for the bad example here. I have updated the indexes array to clear the confusion. Expected Input and Output The input are the three arrays. I wanted to iterate through A, B using the sorted of the indexes array. i.e. I want to find a way to iterate A using the order (a, d, c, b

How to sort an integer array in-place in Python?

ε祈祈猫儿з 提交于 2020-01-22 14:57:08
问题 how can one sort an integer array ( not a list) in-place in Python 2.6? Is there a suitable function in one of the standard libraries? In other words, I'm looking for a function that would do something like this: >>> a = array.array('i', [1, 3, 2]) >>> some_function(a) >>> a array('i', [1, 2, 3]) Thanks in advance! 回答1: Well, you can't do it with array.array , but you can with numpy.array : In [3]: a = numpy.array([0,1,3,2], dtype=numpy.int) In [4]: a.sort() In [5]: a Out[5]: array([0, 1, 2,

How can I sort the columns in a crosstab query, when the column data is dynamic?

你说的曾经没有我的故事 提交于 2020-01-22 14:39:52
问题 I've been doing a bit of research on this topic and I can't seem either find a workable solution, or one that is explained well enough for me to implement. If you've ever created a crosstab query in Access, you are aware that by default Access sorts your columns in alphabetic order. You can change this order by going to the Properties dialog and entering the Column Headings in the order that you prefer. This is a real pain but, as one answerer mentioned on another site, "It's only a pain once

How can I sort the columns in a crosstab query, when the column data is dynamic?

为君一笑 提交于 2020-01-22 14:38:10
问题 I've been doing a bit of research on this topic and I can't seem either find a workable solution, or one that is explained well enough for me to implement. If you've ever created a crosstab query in Access, you are aware that by default Access sorts your columns in alphabetic order. You can change this order by going to the Properties dialog and entering the Column Headings in the order that you prefer. This is a real pain but, as one answerer mentioned on another site, "It's only a pain once

ranking entries in mysql table

自古美人都是妖i 提交于 2020-01-22 09:57:05
问题 I have a MySQL table with many rows. The table has a popularity column. If I sort by popularity, I can get the rank of each item. Is it possible to retrieve the rank of a particular item without sorting the entire table? I don't think so. Is that correct? An alternative would be to create a new column for storing rank, sort the entire table, and then loop through all the rows and update the rank. That is extremely inefficient. Is there perhaps a way to do this in a single query? 回答1: There is