How can I use groupby and mean on a DataFrame, keeping all non-numeric columns?
Example:
ID label_1 label_2 label_3 label_4
0 1 0.582152 13 A False
1 1 0.177475 3 A False
2 2 0.263141 13 B True
3 2 0.630196 3 B True
Expected output:
ID label_1 label_2 label_3 label_4
0 1 0.379814 8.0 A False
1 2 0.446669 8.0 B True
Is there any simple way of doing this? I could get the results I want following this answer but it groups by all non-numerical columns and it doesn't keep the original column order. I wanted to group the data by 'ID' and get the mean of all numeric columns, keeping all other non-numeric columns. Here is my code:
# Group data and get mean
data = data.groupby(list(data.select_dtypes(exclude=np.number))).agg(np.mean).reset_index()
# Reordering the columns to the original order
data = data[[*data.select_dtypes(include=np.number).columns,
*data.select_dtypes(exclude=np.number).columns]]