Created
August 18, 2020 09:09
-
-
Save 3014zhangshuo/a8d8d2f0384c0bfa860ee3ae6defc7d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
result = Hash.new { |h,k| h[k] = Hash.new(0) } | |
CSV.read("file_v1.csv")[1..-1].each do |row| | |
group_id = row[1] | |
black = !row[2].to_i.zero? | |
result[group_id][:total] += 1 | |
result[group_id][:black] += 1 if black | |
end | |
CSV.open("file_v2.csv", 'w') do |csv| | |
csv << %w[Group Percentage] | |
result.each do |group_id, info| | |
csv << [group_id, info[:black].fdiv(info[:total])] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment