Reputation: 261
I have a pandas dataframe with headers id,n and t containing duplicate id and after calling groupby and then size() and extra column with no header is generated given below.Now how do I add an extra column header 'count' associated with the values of the 4th column values so that it becomes ['id','n','t','count']
id n t
7 2 Y 4
7 2 N 6
8 3 Y 2
8 9 N 3
9 1 Y 5
9 6 N 7
Upvotes: 0
Views: 8407
Reputation: 261
As @EdChum mentioned reset_index
has to be called on groupby
and then rename. Pandas generates a header 0 for the extra column
df = df.reset_index()
df.rename(columns={0:'count'}, inplace=True)
Upvotes: 1