pandas reset index after performing groupby and retain selective columns

前端 未结 2 326
逝去的感伤
逝去的感伤 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条回答
  • 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
    
    0 讨论(0)
  • 2021-01-20 21:27

    Use SeriesGroupBy.nunique and filter columns in list after groupby:

    df2 = df.groupby('Ticker')['Date_1','Count','ID'].nunique().reset_index()
    print(df2)
      Ticker  Date_1  Count  ID
    0     AA       1      1   1
    1     BB       2      2   2
    2     CC       2      2   2
    3     DD       1      1   1
    
    0 讨论(0)
提交回复
热议问题