I am trying to groupby a column and compute value counts on another column.
import pandas as pd
dftest = pd.DataFrame({\'A\':[1,1,1,1,1,1,1,1,1,2,2,2,2,2],
You need parameter name
in reset_index, because Series
name is same as name of one of levels of MultiIndex
:
df_grouped.reset_index(name='count')
Another solution is rename Series
name:
print (df_grouped.rename('count').reset_index())
A Amt count
0 1 30 4
1 1 20 3
2 1 40 2
3 2 40 3
4 2 10 2
More common solution instead value_counts
is aggregate size:
df_grouped1 = dftest.groupby(['A','Amt']).size().reset_index(name='count')
print (df_grouped1)
A Amt count
0 1 20 3
1 1 30 4
2 1 40 2
3 2 10 2
4 2 40 3