summer_practice/individual_4.py

57 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import pandas as pd
import matplotlib.pyplot as plt
# разбираем счёт "3:1" на два числа, пустой период считаем как 0
def parse_score(df, col):
parts = df[col].str.split(':', expand=True)
home = pd.to_numeric(parts[0], errors='coerce').fillna(0)
away = pd.to_numeric(parts[1], errors='coerce').fillna(0)
return home, away
def task4(filename):
df = pd.read_csv(filename)
# счёт по периодам, овертайму и буллитам
h1, a1 = parse_score(df, 'Период_1')
h2, a2 = parse_score(df, 'Период_2')
h3, a3 = parse_score(df, 'Период_3')
oth, ota = parse_score(df, 'Овертайм')
soh, soa = parse_score(df, 'Буллиты')
reg_h = h1 + h2 + h3
reg_a = a1 + a2 + a3
# за победу по буллитам команде добавляется одна шайба
tie = (reg_h == reg_a)
win_home_so = tie & (oth == ota) & (soh > soa)
win_away_so = tie & (oth == ota) & (soa > soh)
goals_home = reg_h + oth + win_home_so.astype(int)
goals_away = reg_a + ota + win_away_so.astype(int)
# собираем шайбы обеих команд и суммируем по каждой
home = pd.DataFrame({'Команда': df['Команда_1'], 'ГЗ': goals_home})
away = pd.DataFrame({'Команда': df['Команда_2'], 'ГЗ': goals_away})
both = pd.concat([home, away], ignore_index=True)
totals = both.groupby('Команда')['ГЗ'].sum().astype(int)
totals = totals.sort_values(ascending=False)
# рисуем гистограмму
plt.figure(figsize=(12, 6))
plt.bar(totals.index, totals.values, color='steelblue')
plt.title('Всего заброшено шайб за сезон')
plt.xlabel('Команда')
plt.ylabel('Заброшено шайб')
plt.xticks(rotation=90)
for i, value in enumerate(totals.values):
plt.text(i, value, str(value), ha='center', va='bottom')
plt.tight_layout()
plt.show()
if __name__ == '__main__':
task4(sys.argv[1])