Python Forum

Full Version: requesting help for a faster alternative of pd.groupby
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to python. I have two panda dataframes, they need to be merged conditionally.

group= df.groupby(['index1', 'index2'])['counts'].mean()

for index, value in group.items():
    final_df.loc[(final_df['index1'] == index[0]) & 
           (final_df['index2'] == index[1]), 'result'] = value
For small data sets, above code works just fine, but for a very large dataset, the for loop would take many many hours to complete. Is there any alternative for doing the above task much faster?