[英]Pandas drop unique row in order to use groupby and qcut
How do I drop unique? 我如何独特? It is interfering with groupby and qcut.
它干扰了groupby和qcut。
df0 = psql.read_frame(sql_query,conn)
df = df0.sort(['industry','C'], ascending=[False,True] )
Here is my dataframe: 这是我的数据框:
id industry C
5 28 other industry 0.22
9 32 Specialty Eateries 0.60
10 33 Restaurants 0.84
1 22 Processed & Packaged Goods 0.07
0 21 Processed & Packaged Goods 0.14
8 31 Processed & Packaged Goods 0.43
11 34 Major Integrated Oil & Gas 0.07
14 37 Major Integrated Oil & Gas 0.50
15 38 Independent Oil & Gas 0.06
18 41 Independent Oil & Gas 0.06
19 42 Independent Oil & Gas 0.13
12 35 Independent Oil & Gas 0.43
16 39 Independent Oil & Gas 0.65
17 40 Independent Oil & Gas 0.91
13 36 Independent Oil & Gas 2.25
2 25 Food - Major Diversified 0.35
3 26 Beverages - Soft Drinks 0.54
4 27 Beverages - Soft Drinks 0.73
6 29 Beverages - Brewers 0.19
7 30 Beverages - Brewers 0.21
And I've used the following code from pandas and qcut to rank column 'C' which sadly went batsh*t on me. 而且我使用了来自pandas和qcut的以下代码来对列“ C”进行排名,可悲的是,列对我而言是batsh * t。
df['rank'] = df.groupby(['industry'])['C'].transform(lambda x: pd.qcut(x,5, labels=range(1,6)))
After researching a bit, the reason qcut threw errors is because of the unique value for industry column, reference to error and another ref to err . 经过研究,qcut抛出错误的原因是因为行业专栏的独特价值, 对错误的引用以及对err的另一引用 。
Although, I still want to be able to rank without throwing out unique (unique should be assign to the value of 1) if that is possible. 虽然,我仍然希望能够在不放弃唯一性的情况下进行排名(唯一性应分配给值1)。 But after so many tries, I am convinced that qcut can't handle unique and so I am willing to settle for dropping unique to make qcut happy doing its thing.
但是经过如此多的尝试,我坚信qcut无法处理唯一性,因此我愿意接受放弃唯一性以使qcut开心地完成它的事情。
But if there is another way, I'm very curious to know. 但是,如果还有另一种方法,我很想知道。 I really appreciate your help.
非常感谢您的帮助。
Just in case anyone still wants to do this. 以防万一仍然有人想要这样做。 You should be able to do it by selecting only duplicates?
您应该只选择重复项就能做到吗?
df = df[df['industry'].duplicated(keep=False)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.