I have an Excel sheet file called 'f1-results.xlsx' with tabs named by races (countries names): Australia, China, Bahrain, Russia etc. Every tab contains standings in the following format:
| A | B | C | D | E | F |
|---|---|---|---|---|---|
| 1 | 5 | Sebastian Vettel VET | Ferrari | 1:24:11.672 | 25 |
First column (A) is the position (place after race), next one (B) is non-relevant, another one (C) contains names of the drivers (I need it) and column F contains points scored in this certain race (I need it too). It looks like this:
Sheet (race) 'Australia':
| A | B | C | D | E | F |
|---|---|---|---|---|---|
| 1 | 5 | Sebastian Vettel VET | Ferrari | 1:24:11.672 | 25 |
| 2 | 44 | Lewis Hamilton HAM | Mercedes | +9.975s | 18 |
| 3 | 77 | Valtteri Bottas BOT | Mercedes | +11.250s | 15 |
| 4 | 7 | Kimi Räikkönen RAI | Ferrari | +22.393s | 12 |
| 5 | 33 | Max Verstappen VER | Red Bull Racing TAG Heuer | +28.827s | 10 |
Sheet (race) 'China':
| A | B | C | D | E | F |
|---|---|---|---|---|---|
| 1 | 44 | Lewis Hamilton HAM | Mercedes | 1:37:36.158 | 25 |
| 2 | 5 | Sebastian Vettel VET | Ferrari | +6.250s | 18 |
| 3 | 33 | Max Verstappen VER | Red Bull Racing TAG Heuer | +45.192s | 15 |
| 4 | 3 | Daniel Ricciardo RIC | Red Bull Racing TAG Heuer | +46.035s | 12 |
| 5 | 7 | Kimi Räikkönen RAI | Ferrari | +48.076s | 10 |
| 6 | 77 | Valtteri Bottas BOT | Mercedes | +48.808s | 8 |
Keep in mind that the drivers are listed in different order in every race.
Based on this file, I have to make a new xlsx file with sheet called 'Standings' which will present number of points after each race for each driver.
For example:
| Australia | China | |
|---|---|---|
| Lewis Hamilton | 18 | 43 |
(because he has scored 18 points in Australia race and 25 in China, so 18+25)
Here's my code but I can't make it to sum up the points after each race.
Right now it displays it as below:
| Australia | China | ||
|---|---|---|---|
| Lewis Hamilton | 18 | 25 | 43 |
from openpyxl import load_workbook, Workbook# Load the workbookwb = load_workbook(filename='/kaggle/input/f1-results/f1-results.xlsx')# Create a new workbook for standingsstandings_workbook = Workbook()standings_sheet = standings_workbook.activestandings_sheet.title = 'Standings'# Initialize a dictionary to store points for each driverdriver_points = {}# Iterate through each sheet (race)for sheet_name in wb.sheetnames: # Get the current sheet race_sheet = wb[sheet_name] # Iterate through rows in the current sheet starting from 2nd row (excluding headers) for row in range(2, race_sheet.max_row + 1): # Extract driver name and points driver_name = race_sheet.cell(row=row, column=3).value points = race_sheet.cell(row=row, column=6).value # Update points for the driver if driver_name in driver_points: driver_points[driver_name][sheet_name] = points else: driver_points[driver_name] = {sheet_name: points}# Write headers for each racerace_index = 2for sheet_name in wb.sheetnames: standings_sheet.cell(row=1, column=race_index, value=sheet_name) race_index += 1# Write driver names and points after each racedriver_index = 2for driver, race_points in driver_points.items(): standings_sheet.cell(row=driver_index, column=1, value=driver) race_index = 2 for sheet_name in wb.sheetnames: points = race_points.get(sheet_name, 0) # Get points for this race, or default to 0 if not found standings_sheet.cell(row=driver_index, column=race_index, value=points) race_index += 1 driver_index += 1# Sum up the points for each driverfor row in range(2, standings_sheet.max_row + 1): total_points = sum(int(standings_sheet.cell(row=row, column=col).value or 0) for col in range(2, standings_sheet.max_column + 1)) standings_sheet.cell(row=row, column=standings_sheet.max_column + 1, value=total_points)# Save the standings workbookstandings_workbook.save(filename='_driver_standings.xlsx')I have no idea how to make it add the following cells to each other.