Groupby and Named aggregation | Optimize dataframe generation in Pandas

Pre-aggregating with vectorized methods should be much more efficient (it turns out it was 100x faster):

df['PROMEDIO_EDAD']= df.groupby('CODIGO_SINIESTRO')['EDAD'].transform(np.mean)
df['CANTIDAD_HOMBRES'] = np.where(df['SEXO'] == 'MASCULINO', 1, 0)
df['CANTIDAD_MUJERES'] = np.where(df['SEXO'] == 'FEMENINO', 1, 0)
for col in df['CONDICION'].unique():
    df[col] = np.where(df['CONDICION'] == col, 1, 0)
df = df.groupby(['CODIGO_SINIESTRO', 'PROMEDIO_EDAD']).sum().reset_index().drop('EDAD', axis=1)
df.iloc[:,2:] = (df.iloc[:,2:] > 0).astype(int)
df
Out[1]: 
   CODIGO_SINIESTRO  PROMEDIO_EDAD  CANTIDAD_HOMBRES  CANTIDAD_MUJERES  \
0           4482524           36.5                 1                 1   
1           4486110           33.0                 0                 1   
2          10476434           45.5                 1                 1   

   PASAJERO  CONDUCTOR  MOTOCICLISTA  CICLISTA  PEATON  
0         0          0             1         1       0  
1         0          0             0         0       1  
2         1          1             0         0       0  

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top