import csv def secs(d): t = 0 for p in d.split(":"): t = t * 60 + int(p) return t agg = {} for r in csv.DictReader(open("videos.csv")): a = agg.setdefault(r["channel"], [0, 0.0, 0.0]) s = secs(r["duration"]) a[0] += s if r["views"].strip(): v = float(r["views"].replace(",", "")) a[1] += s / 60 * v a[2] += v w = csv.writer(open("channel_stats.csv", "w", newline="")) w.writerow(["channel", "total_hours", "weighted_minutes"]) for ch, (s, wv, v) in agg.items(): w.writerow([ch, f"{s / 3600:.2f}", f"{wv / v:.2f}"])