我正在尝试创建一个函数,该函数带有一个列表“ teams”,运行一个SQL查询,使用for循环检索每个团队的结果,将结果返回到数据帧中它们各自的字段,然后连接数据端并排。因此理想情况下,结果应如下所示:
我下面的代码产生错误:“'列表'对象没有属性'concat'”。有人可以建议我如何实现期望的输出吗?
谢谢!
teams = ['Chicago','Orlando','Miami','New York']
class Team:
Date = datetime(1900,1,1)
Pts_Scored = 0
def myfunct(conn, teams):
curr = conn.cursor()
pts_scored = []
for t in teams:
curr.execute("select date, pts_scored from db.teams where teams ='"+t+"'")
for i in curr:
point_hist = Team()
point_hist.Date = i[1]
point_hist.Pts_Scored = i[2]
pts_scored.concat(point_hist)
答案 0 :(得分:0)
pts_scored
应该是字典,其键是日期。值应该是另一本字典,其关键字是团队名称,值是分数。
def myfunct(conn, teams):
curr = conn.cursor()
pts_scored = {}
curr.execute("select date, pts_scored, teams from db.teams")
for i in curr:
if i['teams'] in teams:
if i['date'] not in pts_scored:
pts_scores[i['date']] = {}
point_hist[i['date']][i['teams'] = i['pts_scored']
然后您可以使用嵌套循环来打印表:
# print heading
for team in teams:
print("Date", team, end='')
print("")
# print results
for date, results in point_hist.items():
for team in teams
print(date, results[team], end='')
print("")