久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Pandas 按年份分組,按銷售列排名,在具有重復數

pandas group by year, rank by sales column, in a dataframe with duplicate data(Pandas 按年份分組,按銷售列排名,在具有重復數據的數據框中)
本文介紹了Pandas 按年份分組,按銷售列排名,在具有重復數據的數據框中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想創建一個年度排名(所以在 2012 年,經理 B 為 1.在 2011 年,經理 B 再次為 1).我在 pandas rank 函數上苦苦掙扎了一段時間,不想訴諸 for 循環.

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.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 valid with uniquely valued Index objects

有什么想法嗎?
這是我正在使用的真實數據結構.重新索引時遇到問題..

Any ideas?
This is the real data structure I am using. Been having trouble re-indexing..

推薦答案

聽起來你想按Year分組,然后按降序排列Returns.

It sounds like you want to group by the Year, then rank the Returns in descending order.

import pandas as pd
s = pd.DataFrame([['2012', 'A', 3], ['2012', 'B', 8], ['2011', 'A', 20], ['2011', 'B', 30]],
                 columns=['Year', 'Manager', 'Return'])
s['Rank'] = s.groupby(['Year'])['Return'].rank(ascending=False)
print(s)

產量

   Year Manager  Return  Rank
0  2012       A       3     2
1  2012       B       8     1
2  2011       A      20     2
3  2011       B      30     1

<小時>

解決 OP 修改后的問題:錯誤消息


To address the OP's revised question: The error message

ValueError: cannot reindex from a duplicate axis

在嘗試對索引中具有重復值的 DataFrame 進行 groupby/rank 時發生.您可以通過構造 s 在追加后具有唯一索引值來避免該問題:

occurs when trying to groupby/rank on a DataFrame with duplicate values in the index. You can avoid the problem by constructing s to have unique index values after appending:

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, ignore_index=True)

產量

   Year Manager  Return
0  2012       A       3
1  2012       B       8
2  2011       A      20
3  2011       B      30
4  2012       A       3
5  2012       B       8
6  2011       A      20
7  2011       B      30

<小時>

如果您已經使用


If you've already appended new rows using

s = s.append(b)

然后使用 reset_index 創建唯一索引:

then use reset_index to create a unique index:

s = s.reset_index(drop=True)

這篇關于Pandas 按年份分組,按銷售列排名,在具有重復數據的數據框中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 一区二区三区国产精品 | 在线毛片网 | 亚洲欧美日韩精品久久亚洲区 | 免费一区二区在线观看 | 久久精品亚洲精品国产欧美 | 国产精品日韩一区二区 | 久久综合久久综合久久综合 | 成人影院午夜 | 久久久久久蜜桃一区二区 | 在线亚洲免费视频 | 亚洲风情在线观看 | 欧美操操操 | 日韩欧美精品一区 | 国产伦精品一区二区三区精品视频 | 在线观看午夜视频 | 成人免费大片黄在线播放 | 久久久久久久国产精品视频 | 激情福利视频 | 成人三级在线播放 | 久久精品亚洲欧美日韩精品中文字幕 | 亚洲天堂中文字幕 | 欧美一二三四成人免费视频 | 亚洲永久精品国产 | 久久99精品视频 | 99精品欧美 | 免费 视频 1级 | 久久男人| 精品国产一区二区在线 | 一区二区三区日韩精品 | 亚洲成人精品免费 | 日日日日操| 午夜无码国产理论在线 | 欧美一区二区三区在线 | 中文字幕在线一区 | 二区视频| 97精品国产97久久久久久免费 | 国产精品毛片一区二区在线看 | av中文字幕在线观看 | 午夜不卡福利视频 | 国产成人福利在线观看 | 日韩视频在线播放 |