2

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.

Community
  • 1
  • 1
vt2424253
  • 1,387
  • 4
  • 25
  • 39
  • So you want to drop values that are unique? – Woody Pride May 01 '14 at 03:23
  • yes, absolutely. I'm new to pandas and I simply want groupby and qcut to compute ranking for me. So I think that is probably the quickest way to get qcut and groupby to work. But if there is some other way to rank without dropping rows, I love to know. Thanks! – vt2424253 May 01 '14 at 03:36

1 Answers1

0

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)]
lignin
  • 439
  • 1
  • 5
  • 11