简体   繁体   中英

For Loop to Concatenate SQL results

I am trying to create a function that takes a list, 'teams', runs a SQL query retrieving results for each team using a for loop, returns the results to their respective fields in a dataframe, and then concatenates the data side-by-side. So ideally the results would look something like this:

在此处输入图片说明

The code I have below produces an error: "'list' object has no attribute 'concat'". Can someone please advise how I can achieve my desired output?

Thanks!

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)

pts_scored should be a dictionary whose keys are the dates. The values should be another dictionary whose keys are team names and values are the scores.

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']

You can then use a nested loop to print the table:

# 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("")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM