Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

前端 未结 7 1374
无人及你
无人及你 2020-11-21 06:06

I have a data frame df and I use several columns from it to groupby:

df[\'col1\',\'col2\',\'col3\',\'col4\'].groupby([\'col1\',\'co         


        
相关标签:
7条回答
  • 2020-11-21 06:45

    Please try this code

    new_column=df[['col1', 'col2', 'col3', 'col4']].groupby(['col1', 'col2']).count()
    df['count_it']=new_column
    df
    

    I think that code will add a column called 'count it' which count of each group

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