learner
learner

Reputation: 877

Pandas explode a list column, but with extra column, which notes the position in the list

Similar to this question: Pandas column of lists, create a row for each list element

I want to explode a list column, but also get a rank column. None of the answers given in the above question is really doing the job.

So, here is how my data frame looks like:

 df =
         a                     b     

         1                    [2, 4, 6]
         2                    [1, 5, 8, 10]

         ...                   ...

b is a list column.

Here is what I want:

df = 
       a                       b                 rank 
       1                       2                  1
       1                       4                  2
       1                       6                  3
       2                       1                  1
       2                       5                  2
       2                       8                  3
       2                       10                 4
      ...                      ...                ...

Notice, the rank column. rank says the rank of the respective element in the column b. It could also start with 0 index. But, it needs to be there.

Here is what I tried, taking the answer from the question cited:

      res = df.set_index(['a'])['b'].apply(pd.Series).stack()

Now, the above solution would have worked if b was an equal sized list on all rows. But, it isn't. explode would have worked if I did not need the rank column.

So, how to do the above?

Upvotes: 1

Views: 1118

Answers (2)

BENY
BENY

Reputation: 323226

Try with

newdf = df.explode('b')
newdf['rank'] = newdf.groupby('a').cumcount()+1

Upvotes: 2

wwnde
wwnde

Reputation: 26676

df['rank']=df.explode('b').groupby('a')['b'].cumcount()+1

Upvotes: 2

Related Questions