简体   繁体   中英

Pandas drop unique row in order to use groupby and qcut

How do I drop unique? It is interfering with groupby and 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.

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 .

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. 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.

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM