Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

pandas - Group by and Aggregate dataset using Python

I have a dataset, df, where I would like to group by a certain column and take the aggregates of other columns.

df

version     host    date        name    purpose date
pat         A       12/1/2019   pat     hi      12/1/2019
fam         A       12/1/2019   fam     cat     12/1/2019
set         BB      12/2/2019   set     hi      12/2/2020
ok          BB      12/2/2019   ok      hi      12/2/2020
sure        BB      12/2/2019   sure    cat     12/2/2020

DESIRED

version host    date        name    purpose date    
2       A       12/1/2019   2       2       12/1/2019   
3       B       12/2/2019   3       3       12/2/2019   

DOING

df.groupby['host].agg({'version' : 'count', 'name': 'count' , 'purpose': 'count'})

However, this is not giving me the count of the desired columns. Any suggestion is appreciated

question from:https://stackoverflow.com/questions/65837176/group-by-and-aggregate-dataset-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this?

>>> df.groupby('host').agg({'version' : 'count', 'name': 'count' , 'purpose': 'count', 'date': 'first'})
      version  name  purpose       date
host
A           2     2        2  12/1/2019
BB          3     3        3  12/2/2019

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...