ValueError: cannot insert ID, already exists
问题 I have this data: ID TIME 1 2 1 4 1 2 2 3 I want to group the data by ID and calculate the mean time and the size of each group. ID MEAN_TIME COUNT 1 2.67 3 2 3.00 1 If I run this code, then I get an error "ValueError: cannot insert ID, already exists": result = df.groupby(['ID']).agg({'TIME': 'mean', 'ID': 'count'}).reset_index() 回答1: Use parameter drop=True which not create new column with index but remove it: result = df.groupby(['ID']).agg({'TIME': 'mean', 'ID': 'count'}).reset_index(drop