Skip to content Skip to sidebar Skip to footer

Pandas Enumerate Groups In Descending Order

I've the following column: column 0 10 1 10 2 8 3 8 4 6 5 6 My goal is to find the today unique values (3 in this case) and create a new colum

Solution 1:

pd.factorize

i, u = pd.factorize(df.column)
df.assign(new=len(u) - i)

   column  new01031103282382461561

dict.setdefault

d = {}
for k in df.column:
    d.setdefault(k, len(d))

df.assign(new=len(d) - df.column.map(d))

Solution 2:

Use GroupBy.ngroup with ascending=False:

df.groupby('column', sort=False).ngroup(ascending=False)+1031322324151
dtype: int64

For DataFrame that looks like this,

df = pd.DataFrame({'column': [10, 10, 8, 8, 10, 10]})

. . .where only consecutive values are to be grouped, you'll need to modify your grouper:

(df.groupby(df['column'].ne(df['column'].shift()).cumsum(), sort=False)
   .ngroup(ascending=False)
   .add(1))

0    3
1    3
2    2
3    2
4    1
5    1
dtype: int64

Solution 3:

Try with unique and map

df.column.map(dict(zip(df.column.unique(),reversed(range(df.column.nunique())))))+1
Out[350]: 
031322324151
Name: column, dtype: int64

Solution 4:

Acutally, we can use rank with method being dense i.e

dense: like ‘min’, but rank always increases by 1 between groups

df['column'].rank(method='dense')

03.013.022.032.041.051.0

rank version of @cs95's solution would be

df['column'].ne(df['column'].shift()).cumsum().rank(method='dense',ascending=False)

Solution 5:

IIUC, you want groupID of same-values consecutive groups in reversed order. If so, I think this should work too:

df.column.nunique() - df.column.ne(df.column.shift()).cumsum().sub(1)

Out[691]:
031322324151
Name: column, dtype: int32

Post a Comment for "Pandas Enumerate Groups In Descending Order"