I have a dataframe like the one below:
import pandas as pd
df = pd.DataFrame({"firstname": ["John", "Myla", "Lewis", "John", "Myla"],
"lastname": ['Smith', 'Anderson', 'Werner', 'Smith', 'Lewis'],
"ignore_var": [24, np.nan, 21, 99, 26]})
I am trying to see based only in the first two columns (firstname and lastname) how many times each author appears. In my example, everyone including Myla appears one time except for John who appears 2 time. I would like to add this as df['count']
df.groupby(['firstname', 'lastname']).size()
# there is also count() in case it is preferred
I manage to do the aggregation and the count. How can I lastly merge it to the original dataframe?
I would like to keep all rows and simply add it as an extra column.