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

Iterate through rows in excel spreadsheet and find the difference in time when values in a different column are zero

$
0
0

This code iterates through an excel spreadsheet and finds a specific column where energy is zero, then calculates the duration of that zero-energy-value period by calculating difference between first & last appearance of the continuous zero values.

The problem I'm experiencing is: when there are multiple back-to-back rows of zero, the code stalls and never provides an output.

I'm finding it difficult to pinpoint where the issue is. Can I get some help with this?


import datetimeimport openpyxlimport collectionsfrom itertools import islice#import pandasfrom openpyxl.workbook import Workbookcpsd = ("Excel file")cpsd_op = openpyxl.load_workbook(cpsd)cpsd_s1 = cpsd_op['Session-2024']cpsd_dcfc1 = openpyxl.Workbook()sheet_dcfc1 = cpsd_dcfc1["Sheet"]# ^ pulls excel file in, we want to use openpyxl over pandas for excel, since it takes less time# cpsd = session datamax_col_og = cpsd_s1.max_columnmax_row_og = cpsd_s1.max_rowmax_col_nw = sheet_dcfc1.max_columnmax_row_nw = sheet_dcfc1.max_rowprint(max_row_og, max_col_og)for i in range(1, max_col_og+1):    c = cpsd_s1.cell(row = 1, column= i)    sheet_dcfc1.cell(row=1, column=i).value = c.valuefor i in range(1, max_col_og+1):        cell_obj = sheet_dcfc1.cell(row=1, column=i)        print(cell_obj.value)def del_empt_row (sheet):    index_row = []    for i in range(1, sheet.max_row):        # define emptiness of cell        if sheet.cell(i, 1).value is None:            # collect indexes of rows            index_row.append(i)    # loop each index value    for row_del in range(len(index_row)):        sheet.delete_rows(idx=index_row[row_del], amount=1)        # exclude offset of rows through each iteration        index_row = list(map(lambda k: k - 1, index_row))for j in range(1, max_row_og +1):    for i in range(1, max_col_og +1):        c = cpsd_s1.cell(row=j, column=1)        if (c.value == "PP/ Charger 2"):            k = cpsd_s1.cell(row=j, column=i)            sheet_dcfc1.cell(row=j, column=i).value = k.value            #print(k.value)del_empt_row(sheet_dcfc1)def enddate (sheet, row):    #returns the end date of the last row with energy = 0    for row2 in range(row, max_row_og + 1):        if (sheet.cell(row=row2, column=10).value != 0):            return [sheet.cell(row=row2-1, column=5).value,row2-1]        else:            enddate(sheet,row+1)def consume(iterator, n):    #allows us to skip the energy = 0 rows that have already been counted, since python is weird about iteration skipping    #"Advance the iterator n-steps ahead. If n is none, consume entirely."    # Use functions that consume iterators at C speed.    if n is None:        # feed the entire iterator into a zero-length deque        collections.deque(iterator, maxlen=0)    else:        # advance to the empty slice starting at position n        next(islice(iterator, n, n), None)zero_time = datetime.datetime(2023, 1, 1, 00, 00, 00, 00)tot_time = datetime.datetime(2023, 1, 1, 00, 00, 00, 00)#print(tot_time)range_x = enumerate(sheet_dcfc1.iter_rows())for row_num, row in range_x:# calculates total time for t-outage provided that there are no empty rows.    print(row_num)    if (row[9].value == 0):        strt_date = row[2].value        print(strt_date)        strt_row = row_num        end_date_arr = enddate(sheet_dcfc1,row_num+1)        end_date = end_date_arr[0]        print(end_date)        time = end_date-strt_date        consume(range_x, end_date_arr[1]-strt_row)#        print(row_num)        #print(str(row) +"does row change?")        tot_time += time#        print(time)print(tot_time-zero_time)# prints total time for t-outage provided that there are no empty rows.

Viewing all articles
Browse latest Browse all 19054

Trending Articles



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