Skip to content
Advertisement

Add counter as an additional column in Python pandas dataframe

I have following dataframe as an output of my python script. I would like to add another column with count per pmid and add the counter to the first row, keeping the other rows.

The dataframe looks like this:

df

JavaScript

Expected out is:

JavaScript

How can I achieve this output?

Thanks

Advertisement

Answer

You can add count for each row with groupby().transform:

JavaScript

Output:

JavaScript

Now if you really want only count at the first row for each PMID, you can use mask:

JavaScript

Then you would have:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement