1

how to re rank after drop a row in data in pandas

I have a data result from recommender system like this for example

id          content_id       Rank
08773338    aaaa              1
08773338    bbbb              2
08773338    cccc              3
08333777    bbbb              1
08333777    aaaa              2
08333777    cccc              3

then I want to drop 'bbbb' content id so that it becomes like this

for example

id          content_id       Rank
08773338    aaaa              1
08773338    cccc              3
08333777    aaaa              2
08333777    cccc              3

How do I re-rank the rank column after the bbbb label is removed?

like this

id          content_id       Rank
08773338    aaaa              1
08773338    cccc              2
08333777    aaaa              1
08333777    cccc              2

Submitted January 06th 2021 by Admin

Answers
0

You can use groupby.rank after masking/subsetting the dataframe:

u = df[df['content_id'].ne("bbbb")].copy()
u['Rank'] = u.groupby("id")['Rank'].rank(method='dense')#.reset_index(drop=True)

print(u) id content_id Rank
0 8773338 aaaa 1.0
2 8773338 cccc 2.0
4 8333777 aaaa 1.0
5 8333777 cccc 2.0

Admin | 9 months ago


0

You can do this with grp.cumcount:

import pandas as pd
from io import StringIO
df = pd.read_table(StringIO("""id content_id Rank
08773338 aaaa 1
08773338 bbbb 2
08773338 cccc 3
08333777 bbbb 1
08333777 aaaa 2
08333777 cccc 3"""), sep="\s+") df = df.loc[df["content_id"] != "bbbb", :]
df["Rank"] = df.groupby("id").cumcount() + 1

Output:

 id content_id Rank
0 8773338 aaaa 1
2 8773338 cccc 2
4 8333777 aaaa 1
5 8333777 cccc 2

Admin | 9 months ago



Relevant Questions