标签: rank

pandas group by year,按销售列排名,在具有重复数据的数据框中

我想创建一年的排名(因此在2012年,经理B为1. 2011年,经理B再次为1).我和pandas rank函数挣扎了一段时间,并且不想求助于for循环.

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
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是附加代码(之前认为这不相关):

s = pd.DataFrame([['2012', 'A', 3], ['2012', 'B', 8], ['2011', 'A', 20], ['2011', 'B', 30]], columns=['Year', 'Manager', 'Return'])
b = pd.DataFrame([['2012', 'A', 3], ['2012', 'B', 8], ['2011', 'A', 20], ['2011', 'B', 30]], columns=['Year', 'Manager', 'Return'])

s = s.append(b)
s['Rank'] = s.groupby(['Year'])['Return'].rank(ascending=False)

raise Exception('Reindexing only valid with uniquely valued Index '
Exception: Reindexing only …
Run Code Online (Sandbox Code Playgroud)

python duplicates rank pandas pandas-groupby

16
推荐指数
1
解决办法
2万
查看次数

如何在R组内排名?

好的,看看这个数据框......

  customer_name order_dates order_values
1          John  2010-11-01           15
2           Bob  2008-03-25           12
3          Alex  2009-11-15            5
4          John  2012-08-06           15
5          John  2015-05-07           20
Run Code Online (Sandbox Code Playgroud)

假设我想添加一个订单变量,按照名称,按最大订单日期排序最高订单值,使用最后的订单断路器订单日期.所以,最终数据应如下所示:

  customer_name order_dates order_values ranked_order_values_by_max_value_date
1          John  2010-11-01           15                               3
2           Bob  2008-03-25           12                               1
3          Alex  2009-11-15            5                               1
4          John  2012-08-06           15                               2
5          John  2015-05-07           20                               1
Run Code Online (Sandbox Code Playgroud)

每个人的单个订单获得1,并且所有后续订单都根据该值进行排名,并且决胜者是获得优先权的最后订单日期.在这个例子中,John的8/6/2012订单获得了#2等级,因为它是在2010年11月1日之后放置的.2015年5月7日的订单是1,因为它是最大的.因此,即使该订单是在20年前发布的,也应该是#1 Rank,因为它是John的最高订单价值.

有谁知道如何在R中做到这一点?我可以在数据框中的一组指定变量中排名?

谢谢你的帮助!

group-by r rank

15
推荐指数
3
解决办法
2万
查看次数

如何在特定列上排名时进行分区?

所有:

我有一个类似于follow的数据框.我知道我可以做这样的全局排名顺序:

dt <- data.frame(
    ID = c('A1','A2','A4','A2','A1','A4','A3','A2','A1','A3'),
    Value = c(4,3,1,3,4,6,6,1,8,4)
);
> dt
   ID Value
1  A1     4
2  A2     3
3  A4     1
4  A2     3
5  A1     4
6  A4     6
7  A3     6
8  A2     1
9  A1     8
10 A3     4
dt$Order <- rank(dt$Value,ties.method= "first")
> dt
   ID Value Order
1  A1     4     5
2  A2     3     3
3  A4     1     1
4  A2     3     4
5  A1     4     6
6  A4     6     8
7 …
Run Code Online (Sandbox Code Playgroud)

r rank database-partitioning dataframe

14
推荐指数
3
解决办法
2万
查看次数

R中矩阵的秩

我想测试一个矩阵的等级,是否有人可以为此推荐R中的包/函数?

r matrix rank

14
推荐指数
2
解决办法
2万
查看次数

如何跟踪球员的排名?

我有Player一个score属性类:

class Player(game_engine.Player):

    def __init__(self, id):
        super().__init__(id)
        self.score = 0
Run Code Online (Sandbox Code Playgroud)

当玩家成功/未能完成目标时,该分数增加/减少.现在我需要告诉玩家他的排名超出了玩家的总数

print('Your rank is {0} out of {1}')
Run Code Online (Sandbox Code Playgroud)

首先,我想到了所有玩家的列表,以及玩家什么时候发生的事情:

  1. 我检查他的分数是增加还是减少
  2. 在列表中找到他
  3. 移动他直到他的分数在正确的位置

但这将非常缓慢.可以有成千上万的玩家,并且玩家可以重置他自己的分数,0这意味着我必须在堆叠中移动所有人.即使找到玩家也是O(n).

我正在寻找的是一个高性能的解决方案.尽管应该使用常识,但RAM的使用并不那么重要.我怎样才能更快地改进系统?

更新信息:我每次离开游戏服务器时都会使用SQLAlchemy将玩家的数据存储到MySQL数据库中,并且每次他加入服务器时都会加载它.这些是通过'player_join''player_leave'事件处理:

@Event('player_join')
def load_player(id):
    """Load player into the global players dict."""
    session = Session()
    query = session.query(Player).filter_by(id=id)
    players[id] = query.one_or_none() or Player(id=id)

@Event('player_leave')
def save_player(id):
    """Save player into the database."""
    session = Session()
    session.add(players[id])
    session.commit()
Run Code Online (Sandbox Code Playgroud)

此外,玩家的分数会根据'player_kill'事件更新:

@Event('player_kill')
def update_score(id, target_id):
    """Update …
Run Code Online (Sandbox Code Playgroud)

python algorithm ranking rank python-3.x

14
推荐指数
1
解决办法
1322
查看次数

如何以数字向量的降序显示ggplot2中的条形图?

df <- data.frame (Categories=c("Alpha Category", "Alpha Category", 
                               "Alpha Category", "Bravo Category", 
                               "Bravo Category", "Bravo Category", 
                               "Charlie Category", "Charlie Category", 
                               "Charlie Category"),  
                  choices=c("alpha1", "alpha2", "alpha3", "bravo1", 
                            "bravo2", "bravo3", "charlie1", "charlie2",
                            "charlie3")  , 
                  ratings=c(20,60,40, 55,75,25,65,35,45))    
df.plot <- ggplot(df, aes(Categories, ratings, fill = choices))
           + geom_bar(position="dodge", stat="identity") 
           + coord_flip()    
df.plot <- df.plot 
           + theme_classic(base_size = 16, base_family = "")  
           + scale_fill_brewer(palette="Paired")    
df.plot <- df.plot 
           + scale_y_continuous(breaks=seq(0,100,by=10),limits=c(0,80) )  
           + ylab("Ratings")  
           + theme(axis.text.y = element_text(size=16)) #change font size of y axis label   
df.plot
Run Code Online (Sandbox Code Playgroud)

我真的很感激一些帮助

最重要的是,我想按照"评级"的降序显示每个"类别"中的"选择",例如,"查理类别"将显示charlie1,然后是charlie3,然后是charlie2. …

r rank ggplot2

13
推荐指数
2
解决办法
9170
查看次数

排名算法

我有大约4000篇博文.我想根据以下值对所有帖子进行排名

Upvote Count => P
Comments Recieved => C
Share Count => S
Created time in Epoch => E
Follower Count of Category which post belongs to => F (one post has one category)
User Weight => U (User with most number of post have biggest weight)
Run Code Online (Sandbox Code Playgroud)

我期待在伪代码中回答.

algorithm math machine-learning ranking rank

11
推荐指数
2
解决办法
8306
查看次数

Alexa排名与流量估算公式

任何人都知道我们如何转换alexa Rank来估计网站的每日访问者.以前我们可以通过Alexa Site Reach百分比很容易地做到这一点,但alexa覆盖率已不再可用.我之前正在使用这个论坛

$visitors = (200000000*$reach)/100
Run Code Online (Sandbox Code Playgroud)

我们怎么能用alexa排名估计呢?

php traffic rank alexa

11
推荐指数
1
解决办法
2万
查看次数

计算2*2矩阵等级的最快方法?

计算R中矩阵等级的推荐方法似乎是qr:

X <- matrix(c(1, 2, 3, 4), ncol = 2, byrow=T)
Y <- matrix(c(1.0, 1, 1, 1), ncol = 2, byrow=T)
qr(X)$rank
[1] 2
qr(Y)$rank
[1] 1
Run Code Online (Sandbox Code Playgroud)

通过针对我的特定情况修改此函数,我能够提高效率:

qr2 <- function (x, tol = 1e-07) { 
  if (!is.double(x)) 
  storage.mode(x) <- "double"
  p <- as.integer(2)
  n <- as.integer(2)
  res <- .Fortran("dqrdc2", qr = x, n, n, p, as.double(tol),
                  rank = integer(1L), qraux = double(p), pivot = as.integer(1L:p), 
                  double(2 * p), PACKAGE = "base")[c(1, 6, 7, 8)]
  class(res) <- "qr" …
Run Code Online (Sandbox Code Playgroud)

performance r matrix rank

10
推荐指数
1
解决办法
711
查看次数

在TRUE / FALSE和NA分组序列中对组进行排名

我有一点坚果要破解。

我有一个data.frame这样的:

   group criterium
1      A        NA
2      A      TRUE
3      A      TRUE
4      A      TRUE
5      A     FALSE
6      A     FALSE
7      A      TRUE
8      A      TRUE
9      A     FALSE
10     A      TRUE
11     A      TRUE
12     A      TRUE
13     B        NA
14     B     FALSE
15     B      TRUE
16     B      TRUE
17     B      TRUE
18     B     FALSE

structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), …
Run Code Online (Sandbox Code Playgroud)

r rank dplyr data.table

10
推荐指数
3
解决办法
467
查看次数