generated from CMPSC-580-Allegheny-College-Spring-2024/own_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,79 @@ | ||
import csv | ||
import streamlit as st | ||
import pandas as pd | ||
|
||
def sum_csv_part(filename, start_row, end_row, start_col, end_col): | ||
with open(filename, 'r') as file: | ||
csv_reader = csv.reader(file) | ||
data = list(csv_reader) | ||
|
||
# Ensure start_row and end_row are within the range of rows | ||
start_row = max(0, min(start_row, len(data)-1)) | ||
end_row = max(start_row, min(end_row, len(data)-1)) | ||
|
||
# Ensure start_col and end_col are within the range of columns | ||
start_col = max(0, min(start_col, len(data[0])-1)) | ||
end_col = max(start_col, min(end_col, len(data[0])-1)) | ||
|
||
sums = [] | ||
for i in range(start_row, end_row + 1): | ||
row_sum = 0 # Initialize sum variable for each row | ||
for j in range(start_col, end_col + 1): | ||
try: | ||
row_sum += float(data[i][j]) # Accumulate values for each row | ||
except ValueError: | ||
print("ValueError: Invalid literal for float():", data[i][j]) | ||
print("Sum of values in row", i+1, ":", row_sum) | ||
row_sum = sum(float(data[i][j]) for j in range(start_col, end_col + 1)) | ||
sums.append(row_sum) | ||
return sums | ||
|
||
def multiply_csv_part(filename, start_row, end_row, start_col, end_col): | ||
with open(filename, 'r') as file: | ||
csv_reader = csv.reader(file) | ||
data = list(csv_reader) | ||
|
||
# Ensure start_row and end_row are within the range of rows | ||
start_row = max(0, min(start_row, len(data)-1)) | ||
end_row = max(start_row, min(end_row, len(data)-1)) | ||
|
||
# Ensure start_col and end_col are within the range of columns | ||
start_col = max(0, min(start_col, len(data[0])-1)) | ||
end_col = max(start_col, min(end_col, len(data[0])-1)) | ||
|
||
products = [] | ||
for i in range(start_row, end_row + 1): | ||
row_product = 1 # Initialize product variable for each row as 1 | ||
for j in range(start_col, end_col + 1): | ||
try: | ||
row_product *= float(data[i][j]) # Accumulate values for each row | ||
except ValueError: | ||
print("ValueError: Invalid literal for float():", data[i][j]) | ||
print("Product of values in row", i+1, ":", row_product) | ||
|
||
sum_csv_part('data.csv', 1, 3, 6, 7) | ||
multiply_csv_part('data.csv', 1, 3, 3, 4) | ||
row_product = float(data[i][start_col]) * float(data[i][end_col]) | ||
products.append(row_product) | ||
return products | ||
|
||
def divide_csv_part(filename, start_row, end_row, start_col, end_col): | ||
sums = sum_csv_part(filename, start_row, end_row, start_col, end_col) | ||
products_1 = multiply_csv_part(filename, start_row, end_row, 3, 4) | ||
products_2 = multiply_csv_part(filename, start_row, end_row, 3, 5) | ||
|
||
division_results_1 = [] | ||
division_results_2 = [] | ||
for sum_val, prod_val in zip(sums, products_1): | ||
if sum_val != 0: | ||
division_results_2.append(prod_val / sum_val) | ||
else: | ||
division_results_2.append(float('NaN')) | ||
|
||
for sum_val, prod_val in zip(sums, products_2): | ||
if sum_val != 0: | ||
division_results_1.append(prod_val / sum_val) | ||
else: | ||
division_results_1.append(float('NaN')) | ||
|
||
st.title("Swing Efficiency %") | ||
|
||
a = {'Names': print_csv_row_range(filename, 1, 135),'Swing Efficiency %': division_results_1} | ||
df = pd.DataFrame.from_dict(a, orient='index') | ||
df = df.transpose() | ||
|
||
sort_column = st.selectbox('Select column to sort by:', df.columns) | ||
df_sorted = df.sort_values(by=sort_column, ascending=False) | ||
|
||
st.table(df_sorted) | ||
|
||
def print_csv_row_range(filename, start_row_index, end_row_index): | ||
with open(filename, 'r') as file: | ||
csv_reader = csv.reader(file) | ||
rows = list(csv_reader) | ||
result = [] | ||
for row_index in range(start_row_index, min(end_row_index + 1, len(rows))): | ||
result.append(rows[row_index][0]) # Exclude the last element of the row | ||
return result | ||
|
||
|
||
divide_csv_part('data.csv', 1, 135, 6, 7) |