pandas reset_index after groupby.value_counts()

前端 未结 1 1860
情话喂你
情话喂你 2021-01-31 17:19

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], 
             


        
1条回答
  •  不思量自难忘°
    2021-01-31 17:42

    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
    

    0 讨论(0)
提交回复
热议问题