I'm attempting to get the count of each unique value for a given column. For example, I'd like to get the following results from the dataframe:
df = pd.DataFrame.from_dict({'id': [1,1,2,3,3,3], 'a': [1,2,3,4,5,6], 'b': [6,5,4,3,2,1]})
What I want is a dataframe that looks like:
pd.DataFrame.from_dict({'id': [1,2,3], 'count': [2, 1, 3]})
I found that I can get close to this with:
df.groupby('id', as_index=False).agg('count')[['id', 'a']]
But I'm looking for a way that's a bit more descriptive of what is happening / "pythonic". Additionally, I'd have to do a column rename as well. Is there a better way to accomplish this?