【发布时间】:2018-09-10 08:28:54
【问题描述】:
我有一些事故数据,我试图通过 SQLite3 在 Python 中查询。我试图确定一周中哪一天在特定的年月对中发生的事故最多。数据有以下字段:
事故:Accident_Index |年份 |月 | Day_of_Week
其中 Day_of_Week 的范围为 1-7,周日为 1。结果应按年升序排列,然后按月排列,新表应包含以下列:
max_dayofweek: 年 |月 | Day_of_Week | Num_of_Accidents
到目前为止,我有以下内容:
query = '''
SELECT Year, Month, Day_of_Week, count(*) as Num_of_Accidents
from Accidents
group by Year, Month, Day_of_Week
order by Year, Month
'''
max_dayofweek = pd.read_sql_query(query, conn)
但这会返回:
Year Month Day_of_Week Num_of_Accidents
0 2009 1 1 623
1 2009 1 2 1095
2 2009 1 3 1197
3 2009 1 4 1244
4 2009 1 5 1123
5 2009 1 6 1404
6 2009 1 7 1114
7 2009 2 1 592
8 2009 2 2 919
9 2009 2 3 887
10 2009 2 4 1013
11 2009 2 5 915
12 2009 2 6 1032
13 2009 2 7 824
但是输出应该是这样的:
Year Month Day_of_Week Num_of_Accidents
0 2009 1 6 1404
1 2009 2 6 1032
等等……
任何帮助将不胜感激!
【问题讨论】:
标签: python sql python-3.x sqlite