rank

pandas group by year, rank by sales column, in a dataframe with duplicate data

ぐ巨炮叔叔 提交于 2019-11-26 19:38:25
问题 I would like to create a rank on year (so in year 2012, Manager B is 1. In 2011, Manager B is 1 again). I struggled with the pandas rank function for awhile and DO NOT want to resort to a for loop. s = pd.DataFrame([['2012','A',3],['2012','B',8],['2011','A',20],['2011','B',30]], columns=['Year','Manager','Return']) Out[1]: Year Manager Return 0 2012 A 3 1 2012 B 8 2 2011 A 20 3 2011 B 30 The issue I'm having is with the additional code (didn't think this would be relevant before): s = pd

Unique Rank value for a subgroup within a group

点点圈 提交于 2019-11-26 17:48:43
问题 I am trying to get a unique rank value (e.g. {1, 2, 3, 4} from a subgroup in my data. SUMPRODUCT will produce ties{1, 1, 3, 4} , I am trying to add the COUNTIFS to the end to adjust the duplicate rank away. subgroup col B col M rank LMN 01 1 XYZ 02 XYZ 02 ABC 03 ABC 01 XYZ 01 LMN 02 3 ABC 01 LMN 03 4 LMN 03 4 'should be 5 ABC 02 XYZ 02 LMN 01 1 'should be 2 So far, I've come up with this. =SUMPRODUCT(($B$2:$B$38705=B2)*(M2>$M$2:$M$38705))+countifs(B2:B38705=B2,M2:M38705=M2) What have I done

How to Block an IP address range using the .htaccess file

若如初见. 提交于 2019-11-26 15:55:48
问题 I have detected that a range of IP addresses may be used in a malicious way and I don't know how to block it. I would like to block the range 66.249.74.* from accessing my website by using the .htaccess file. 回答1: You could use: Order Allow,Deny Deny from 66.249.74.0/24 Allow from all Or you could use this: RewriteEngine on RewriteCond %{REMOTE_ADDR} ^66\.249\.74\. RewriteRule ^ - [F] 回答2: Use just the first 3 octets Order Allow,Deny Deny from 66.249.74. Allow from all 回答3: I’ve just used

Rank() over Partition by in mysql

南笙酒味 提交于 2019-11-26 14:48:09
问题 I'm completely stumped as to create a new column "LoginRank" from rank() over(partition by x, order by y desc) in mysql. From sql server i would write the following query, to create a column "Loginrank" that is grouped by "login" and ordered by "id". select ds.id, ds.login, rank() over(partition by ds.login order by ds.id asc) as LoginRank from tablename.ds I have the following table. create table ds (id int(11), login int(11)) insert into ds (id, login) values (1,1), (2,1), (3,1), (4,2), (5

Calculating percentile rank in MySQL

别来无恙 提交于 2019-11-26 13:58:55
问题 I have a very big table of measurement data in MySQL and I need to compute the percentile rank for each and every one of these values. Oracle appears to have a function called percent_rank but I can't find anything similar for MySQL. Sure I could just brute-force it in Python which I use anyways to populate the table but I suspect that would be quite inefficient because one sample might have 200.000 observations. 回答1: This is a relatively ugly answer, and I feel guilty saying it. That said,

MySQL, Get users rank

自古美人都是妖i 提交于 2019-11-26 09:09:58
问题 I have a mysql table like below: id name points 1 john 4635 3 tom 7364 4 bob 234 6 harry 9857 I basically want to get an individual user rank without selecting all of the users. I only want to select a single user by id and get the users rank which is determined by the number of points they have. For example, get back tom with the rank 2 selecting by the id 3. Cheers Eef 回答1: SELECT uo.*, ( SELECT COUNT(*) FROM users ui WHERE (ui.points, ui.id) >= (uo.points, uo.id) ) AS rank FROM users uo

Get records with highest/smallest <whatever> per group

强颜欢笑 提交于 2019-11-26 00:57:32
问题 How to do that? Former title of this question was \" using rank (@Rank := @Rank + 1) in complex query with subqueries - will it work? \" because I was looking for solution using ranks, but now I see that the solution posted by Bill is much much better. Original question: I\'m trying to compose a query that would take last record from each group given some defined order: SET @Rank=0; select s.* from (select GroupId, max(Rank) AS MaxRank from (select GroupId, @Rank := @Rank + 1 AS Rank from

Rank function in MySQL

血红的双手。 提交于 2019-11-25 22:14:43
问题 I need to find out rank of customers. Here I am adding the corresponding ANSI standard SQL query for my requirement. Please help me to convert it to MySQL . SELECT RANK() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender], FirstName, Age, Gender FROM Person Is there any function to find out rank in MySQL? 回答1: One option is to use a ranking variable, such as the following: SELECT first_name, age, gender, @curRank := @curRank + 1 AS rank FROM person p, (SELECT @curRank := 0) r

ROW_NUMBER() in MySQL

南笙酒味 提交于 2019-11-25 22:13:09
问题 Is there a nice way in MySQL to replicate the SQL Server function ROW_NUMBER() ? For example: SELECT col1, col2, ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col3 DESC) AS intRow FROM Table1 Then I could, for example, add a condition to limit intRow to 1 to get a single row with the highest col3 for each (col1, col2) pair. 回答1: I want the row with the single highest col3 for each (col1, col2) pair. That's a groupwise maximum, one of the most commonly-asked SQL questions (since it seems

Get records with highest/smallest <whatever> per group

妖精的绣舞 提交于 2019-11-25 20:20:01
How to do that? Former title of this question was " using rank (@Rank := @Rank + 1) in complex query with subqueries - will it work? " because I was looking for solution using ranks, but now I see that the solution posted by Bill is much much better. Original question: I'm trying to compose a query that would take last record from each group given some defined order: SET @Rank=0; select s.* from (select GroupId, max(Rank) AS MaxRank from (select GroupId, @Rank := @Rank + 1 AS Rank from Table order by OrderField ) as t group by GroupId) as t join ( select *, @Rank := @Rank + 1 AS Rank from