ValueError: cannot insert ID, already exists

Question:

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()
Asked By: Dinosaurius

||

Answers:

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=True)
print (result)
   ID      TIME
0   3  2.666667
1   1  3.000000

But if need new column from index need rename old column names first:

result = df.groupby(['ID']).agg({'TIME': 'mean', 'ID': 'count'})
           .rename(columns={'ID':'COUNT','TIME':'MEAN_TIME'})
           .reset_index()
print (result)
   ID  COUNT  MEAN_TIME
0   1      3   2.666667
1   2      1   3.000000

Solution if need aggreagate by multiple columns:

result = df.groupby(['ID']).agg({'TIME':{'MEAN_TIME': 'mean'}, 'ID': {'COUNT': 'count'}})
result.columns = result.columns.droplevel(0)
print (result.reset_index())
   ID  COUNT  MEAN_TIME
0   1      3   2.666667
1   2      1   3.000000
Answered By: jezrael

I’d limit my groupby to just the TIME column.

df.groupby(['ID']).TIME.agg({'MEAN_TIME': 'mean', 'COUNT': 'count'}).reset_index()

   ID  MEAN_TIME  COUNT
0   1   2.666667      3
1   2   3.000000      1
Answered By: piRSquared

You can also assign a copy of the grouping column prior to grouping:

df.assign(id_=df['ID']).groupby(['ID']).agg({'TIME': 'mean', 'id_': 'count'}).reset_index()
Answered By: Andreas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.