Aggregate CSV amounts into regional totals

Ruby
require 'csv'

# Sum the amount column per region; blank or malformed rows are skipped.
def totals_by_region(csv_text)
  totals = Hash.new(0.0)
  CSV.parse(csv_text, headers: true) do |row|
    region = row['region'].to_s.strip
    amount = Float(row['amount'].to_s, exception: false)
    next if region.empty? || amount.nil?
    totals[region] += amount
  end
  totals.sort_by { |_region, total| -total }.to_h
end

sample = <<~CSV
  region,amount
  north,12.50
  south,8.25
  north,3.75
CSV

totals_by_region(sample).each do |region, total|
  puts format('%-8s %8.2f', region, total)
end