在Python

时间:2016-02-26 00:01:38

标签: python

假设我有以下数据,包括姓名,类和日期

Name    class   Date
A   7th grade   1/1/2016
A   7th grade   1/2/2016
A   7th grade   1/3/2016
A   7th grade   1/4/2016
A   7th grade   1/5/2016
A   7th grade   1/6/2016
A   7th grade   1/7/2016
B   8th grade   1/8/2016
B   8th grade   1/9/2016
B   8th grade   1/10/2016
C   9th grade   1/11/2016
C   9th grade   1/12/2016
C   9th grade   1/13/2016
C   9th grade   1/14/2016
C   9th grade   1/15/2016
C   9th grade   1/16/2016
C   9th grade   1/17/2016
C   9th grade   1/18/2016
C   9th grade   1/19/2016
C   9th grade   1/20/2016
C   9th grade   1/21/2016
C   9th grade   1/22/2016

我正在寻找一个输出,它会给我每个名字的值,它们各自的等级,最早的日期和最新的日期。我的输出是,

Name grade   count earlydate latestdate
A   7thgrade   7    1/1/2016  1/7/2016
B   8th grade  3    1/8/2016  1/10/2016
C   9th grade  12   1/11/2016 1/22/2016

我可以通过

找到每个名字的计数
data.groupby('name','grade').count()
or
data.groupby('name','grade').size()

但无法在日期栏中找到早期日期和最新日期。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

Max and Min date in pandas groupby

这样的事情可能是:

data.groupby('name','grade').agg({'date' : [np.min, np.max]}).count()