Search code examples
pythonpandasdataframeindexingpandas-groupby

How to keep original index of a DataFrame after groupby 2 columns?


Is there any way I can retain the original index of my large dataframe after I perform a groupby? The reason I need to this is because I need to do an inner merge back to my original df (after my groupby) to regain those lost columns. And the index value is the only 'unique' column to perform the merge back into. Does anyone know how I can achieve this?

My DataFrame is quite large. My groupby looks like this:

df.groupby(['col1', 'col2']).agg({'col3': 'count'}).reset_index()

This drops my original indexes from my original dataframe, which I want to keep.


Solution

  • I think you are are looking for transform in this situation:

    df['count'] = df.groupby(['col1', 'col2'])['col3'].transform('count')