pandas reset index after performing groupby and retain selective columns

前端 未结 2 327
逝去的感伤
逝去的感伤 2021-01-20 20:36

I want to take a pandas dataframe, do a count of unique elements by a column and retain 2 of the columns. But I get a multi-index dataframe after groupby which I am unable t

2条回答
  •  梦毁少年i
    2021-01-20 21:22

    Try this instead:

    1) Select only the relevant columns (['ID', 'Random_data'])

    2) Don't pass a list to .agg - just 'nunique' - the list is what is causing the multi index behaviour.

    df2 = df.groupby(['Ticker'])['ID', 'Random_data'].agg('nunique')
    df2.reset_index()
    
      Ticker  ID  Random_data
    0     AA   1            1
    1     BB   2            2
    2     CC   2            2
    3     DD   1            1
    

提交回复
热议问题