# Count distinct values in csv

**URL:** <https://www.ruby-forum.com/t/count-distinct-values-in-csv/89851>\
**Category:** Ruby\
**Created:** [March 23, 2007, 10:46am UTC](https://www.ruby-forum.com/t/count-distinct-values-in-csv/89851 "2007-03-23T10:46:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 23, 2007, 10:46am UTC](https://www.ruby-forum.com/t/count-distinct-values-in-csv/89851/1 "2007-03-23T10:46:09Z")

</div>

Hi,

i’m new to ruby.  
How could i read a tab-delimted textfile with two columns + header  
row  
and count the distinct values from the 2.column?

## my first experiments missed because i have to elimnate the header

row!?#######

filename = gets.chomp  
text = String.new  
File.open(filename) { |f| text = f.read }  
values = text.split(/\t/)  
freqs = Hash.new(0)  
values.each { |values| freqs[values] += 1 }  
freqs = freqs.sort\_by {|x,y| y }  
freqs.each {|values, freq| puts values+’ '+freq.to\_s}

Many thanks for a starting point.  
regards, christian

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 23, 2007, 12:38pm UTC](https://www.ruby-forum.com/t/count-distinct-values-in-csv/89851/2 "2007-03-23T12:38:25Z")

</div>

Consider using either the built in CSV parser in Ruby stdlib or  
FasterCSV ( [http://fastercsv.rubyforge.org/](http://fastercsv.rubyforge.org/) ). It’ll save you time in  
the long run.

V/r  
Anthony E.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 23, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/count-distinct-values-in-csv/89851/3 "2007-03-23T15:04:53Z")

</div>

Christian Schulz wrote:

> Hi,
> 
> i’m new to ruby.  
> How could i read a tab-delimted textfile with two columns + header  
> row  
> and count the distinct values from the 2.column?

Christian -

Strangely enough, I wrote a blog post about this very topic:

> **[CSV Manipulation w/ Ruby](https://drewolson.wordpress.com/2007/03/13/csv-manipulation-w-ruby/)**
>
> Lately, I’ve been trying to follow why’s advice by using Ruby at work whenever possible. I love Ruby because it’s very friendly to those who have little (or no) programming backgr…

Here’s the code linked to by the blog post:

require ‘faster\_csv’

unique\_count = {}

FCSV.foreach(“myfile.csv”, :headers =\> true) do |row|  
unique\_count[row[1]] ||= 0  
unique\_count[row[1]] += 1  
end

unique\_count.each do |val,count|  
puts “#{val} appreas #{count} time(s)”  
end

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 23, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/count-distinct-values-in-csv/89851/4 "2007-03-23T19:26:02Z")

</div>

many thanks , ruby is really great and i have to learn think “easy”!  
christian
