Skip to content

Instantly share code, notes, and snippets.

@3014zhangshuo
Created August 18, 2020 09:09
Show Gist options
  • Save 3014zhangshuo/a8d8d2f0384c0bfa860ee3ae6defc7d2 to your computer and use it in GitHub Desktop.
Save 3014zhangshuo/a8d8d2f0384c0bfa860ee3ae6defc7d2 to your computer and use it in GitHub Desktop.
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