Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中查找每个名称的计数、最早日期和最晚日期_Python - Fatal编程技术网

在Python中查找每个名称的计数、最早日期和最晚日期

在Python中查找每个名称的计数、最早日期和最晚日期,python,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

假设我有一个如下的数据,包含名称、类和日期

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()
但无法在“日期”列中找到“最早日期”和“最晚日期”

有人能帮我吗?

可能是这样的:

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

您使用的是哪种数据库?尝试使用联接,sqlI中的MIN/MAX函数将其作为python数据帧。你需要在家里做这件事吗python@clemkoa谢谢你的链接。但在该链接中,日期作为索引而不是列,也不会返回我想要的结果。我想要一个数据框,它会给出这些细节,比如shownok,那么也许?