require 'csv' Hash schoolHash = Hash . new Hash schoolHashAverages = Hash . new CSV. foreach ( "C: \\ CollegeBall \\ all_ncaa_tourney_players_14_player_metadata.csv" , :headers = & gt; true ) do | row | school = row [ 8 ] weight = row [ 5 ] . to_i if schoolHash. has_key ? ( school ) count = schoolHash [ school ] count + = 1 schoolHash [ school ] = count countWeight = schoolHashAverages [ school ] countWeight + = weight schoolHashAverages [ school ] = countWeight else schoolHash [ school ] = 1 schoolHashAverages [ school ] = weight end end Hash averagesHash = Hash . new schoolHashAverages. each do | key, value | averagesHash [ key ] = ( value. to_f / schoolHash [ key ] . to_f ) . round ( 2 ) end averagesHash = averagesHash. sort_by { | _key, value | value } averagesHash. each do | key, value | puts ",[ '#{key.delete ' \' '}', #{value} ]" end