Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Summing up cells value with openpyxl

$
0
0

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:

ABCDEF
15Sebastian Vettel VETFerrari1:24:11.67225

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

ABCDEF
15Sebastian Vettel VETFerrari1:24:11.67225
244Lewis Hamilton HAMMercedes+9.975s18
377Valtteri Bottas BOTMercedes+11.250s15
47Kimi Räikkönen RAIFerrari+22.393s12
533Max Verstappen VERRed Bull Racing TAG Heuer+28.827s10

Sheet (race) 'China':

ABCDEF
144Lewis Hamilton HAMMercedes1:37:36.15825
25Sebastian Vettel VETFerrari+6.250s18
333Max Verstappen VERRed Bull Racing TAG Heuer+45.192s15
43Daniel Ricciardo RICRed Bull Racing TAG Heuer+46.035s12
57Kimi Räikkönen RAIFerrari+48.076s10
677Valtteri Bottas BOTMercedes+48.808s8

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:

AustraliaChina
Lewis Hamilton1843

(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:

AustraliaChina
Lewis Hamilton182543
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.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>