Ruby - Grouping data from CSV file by a column











up vote
1
down vote

favorite












I have a CSV file that looks like this:



+---------+----------+
| Name | Stream |
+---------+----------+
| Jacob | Computer |
| Ryan | Arts |
| Bob | Computer |
| Charlie | Science |
| Grace | Arts |
+---------+----------+


I need to read this CSV file and group the students based on their stream. The output should be like this:



Computer
----------
Jacob
Bob

Arts
------
Ryan
Grace

Science
--------
Charlie


I tried to use group_by, but was not sure how and where to use it. Any help would be much appreciated.










share|improve this question
























  • Is that what you literally have? Then that is not a CSV file.
    – sawa
    2 days ago










  • Show what you tried.
    – sawa
    2 days ago










  • What I have is indeed a csv file; I have just put it down in a tabular format so that it looks better and is easier to read and understand.
    – Archana K
    2 days ago










  • Also as I mentioned, since I am new to the language I am not sure on the approach that I should take to get this done.
    – Archana K
    2 days ago















up vote
1
down vote

favorite












I have a CSV file that looks like this:



+---------+----------+
| Name | Stream |
+---------+----------+
| Jacob | Computer |
| Ryan | Arts |
| Bob | Computer |
| Charlie | Science |
| Grace | Arts |
+---------+----------+


I need to read this CSV file and group the students based on their stream. The output should be like this:



Computer
----------
Jacob
Bob

Arts
------
Ryan
Grace

Science
--------
Charlie


I tried to use group_by, but was not sure how and where to use it. Any help would be much appreciated.










share|improve this question
























  • Is that what you literally have? Then that is not a CSV file.
    – sawa
    2 days ago










  • Show what you tried.
    – sawa
    2 days ago










  • What I have is indeed a csv file; I have just put it down in a tabular format so that it looks better and is easier to read and understand.
    – Archana K
    2 days ago










  • Also as I mentioned, since I am new to the language I am not sure on the approach that I should take to get this done.
    – Archana K
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a CSV file that looks like this:



+---------+----------+
| Name | Stream |
+---------+----------+
| Jacob | Computer |
| Ryan | Arts |
| Bob | Computer |
| Charlie | Science |
| Grace | Arts |
+---------+----------+


I need to read this CSV file and group the students based on their stream. The output should be like this:



Computer
----------
Jacob
Bob

Arts
------
Ryan
Grace

Science
--------
Charlie


I tried to use group_by, but was not sure how and where to use it. Any help would be much appreciated.










share|improve this question















I have a CSV file that looks like this:



+---------+----------+
| Name | Stream |
+---------+----------+
| Jacob | Computer |
| Ryan | Arts |
| Bob | Computer |
| Charlie | Science |
| Grace | Arts |
+---------+----------+


I need to read this CSV file and group the students based on their stream. The output should be like this:



Computer
----------
Jacob
Bob

Arts
------
Ryan
Grace

Science
--------
Charlie


I tried to use group_by, but was not sure how and where to use it. Any help would be much appreciated.







ruby csv group-by






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









VAD

1,69131324




1,69131324










asked 2 days ago









Archana K

112




112












  • Is that what you literally have? Then that is not a CSV file.
    – sawa
    2 days ago










  • Show what you tried.
    – sawa
    2 days ago










  • What I have is indeed a csv file; I have just put it down in a tabular format so that it looks better and is easier to read and understand.
    – Archana K
    2 days ago










  • Also as I mentioned, since I am new to the language I am not sure on the approach that I should take to get this done.
    – Archana K
    2 days ago


















  • Is that what you literally have? Then that is not a CSV file.
    – sawa
    2 days ago










  • Show what you tried.
    – sawa
    2 days ago










  • What I have is indeed a csv file; I have just put it down in a tabular format so that it looks better and is easier to read and understand.
    – Archana K
    2 days ago










  • Also as I mentioned, since I am new to the language I am not sure on the approach that I should take to get this done.
    – Archana K
    2 days ago
















Is that what you literally have? Then that is not a CSV file.
– sawa
2 days ago




Is that what you literally have? Then that is not a CSV file.
– sawa
2 days ago












Show what you tried.
– sawa
2 days ago




Show what you tried.
– sawa
2 days ago












What I have is indeed a csv file; I have just put it down in a tabular format so that it looks better and is easier to read and understand.
– Archana K
2 days ago




What I have is indeed a csv file; I have just put it down in a tabular format so that it looks better and is easier to read and understand.
– Archana K
2 days ago












Also as I mentioned, since I am new to the language I am not sure on the approach that I should take to get this done.
– Archana K
2 days ago




Also as I mentioned, since I am new to the language I am not sure on the approach that I should take to get this done.
– Archana K
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I assume you have csv file with the following content:



Name,Stream
Jacob,Computer
Ryan,Arts
Bob,Computer
Charlie,Science
Grace,Arts


You can use something like this



require 'csv'    

result = {}
file = File.read('path_to_your_file')
csv = CSV.parse(file, headers: true)
csv.each do |row|
if result[row[1]]
result[row[1]].push row[0]
else
result[row[1]] = [row[0]]
end
end


You'll get result variable containing a hash where every stream will be associated with an array of names






share|improve this answer























  • Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
    – Archana K
    2 days ago












  • result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
    – VAD
    2 days ago










  • Perfect! You might want to remove the . character in the puts.key statement
    – Archana K
    2 days ago










  • sure, but I can't :)
    – VAD
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203820%2fruby-grouping-data-from-csv-file-by-a-column%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I assume you have csv file with the following content:



Name,Stream
Jacob,Computer
Ryan,Arts
Bob,Computer
Charlie,Science
Grace,Arts


You can use something like this



require 'csv'    

result = {}
file = File.read('path_to_your_file')
csv = CSV.parse(file, headers: true)
csv.each do |row|
if result[row[1]]
result[row[1]].push row[0]
else
result[row[1]] = [row[0]]
end
end


You'll get result variable containing a hash where every stream will be associated with an array of names






share|improve this answer























  • Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
    – Archana K
    2 days ago












  • result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
    – VAD
    2 days ago










  • Perfect! You might want to remove the . character in the puts.key statement
    – Archana K
    2 days ago










  • sure, but I can't :)
    – VAD
    2 days ago















up vote
0
down vote



accepted










I assume you have csv file with the following content:



Name,Stream
Jacob,Computer
Ryan,Arts
Bob,Computer
Charlie,Science
Grace,Arts


You can use something like this



require 'csv'    

result = {}
file = File.read('path_to_your_file')
csv = CSV.parse(file, headers: true)
csv.each do |row|
if result[row[1]]
result[row[1]].push row[0]
else
result[row[1]] = [row[0]]
end
end


You'll get result variable containing a hash where every stream will be associated with an array of names






share|improve this answer























  • Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
    – Archana K
    2 days ago












  • result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
    – VAD
    2 days ago










  • Perfect! You might want to remove the . character in the puts.key statement
    – Archana K
    2 days ago










  • sure, but I can't :)
    – VAD
    2 days ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I assume you have csv file with the following content:



Name,Stream
Jacob,Computer
Ryan,Arts
Bob,Computer
Charlie,Science
Grace,Arts


You can use something like this



require 'csv'    

result = {}
file = File.read('path_to_your_file')
csv = CSV.parse(file, headers: true)
csv.each do |row|
if result[row[1]]
result[row[1]].push row[0]
else
result[row[1]] = [row[0]]
end
end


You'll get result variable containing a hash where every stream will be associated with an array of names






share|improve this answer














I assume you have csv file with the following content:



Name,Stream
Jacob,Computer
Ryan,Arts
Bob,Computer
Charlie,Science
Grace,Arts


You can use something like this



require 'csv'    

result = {}
file = File.read('path_to_your_file')
csv = CSV.parse(file, headers: true)
csv.each do |row|
if result[row[1]]
result[row[1]].push row[0]
else
result[row[1]] = [row[0]]
end
end


You'll get result variable containing a hash where every stream will be associated with an array of names







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









VAD

1,69131324




1,69131324












  • Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
    – Archana K
    2 days ago












  • result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
    – VAD
    2 days ago










  • Perfect! You might want to remove the . character in the puts.key statement
    – Archana K
    2 days ago










  • sure, but I can't :)
    – VAD
    2 days ago


















  • Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
    – Archana K
    2 days ago












  • result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
    – VAD
    2 days ago










  • Perfect! You might want to remove the . character in the puts.key statement
    – Archana K
    2 days ago










  • sure, but I can't :)
    – VAD
    2 days ago
















Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
– Archana K
2 days ago






Great, that works beautifully. Right now my output is {"Computer"=>["Jacob", "Bob"], "Arts"=>["Ryan", "Grace"], "Science"=>["Charlie"]} What would help is if I could get it in the format of course name followed by the student names in separate lines (as illustrated in the question). I am trying to do that but the fact that the 'result' is an array seems to be making it slightly more complicated.
– Archana K
2 days ago














result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
– VAD
2 days ago




result.keys.each { |key| puts; puts.key; puts '-----'; result[key].each { |val| puts val } }
– VAD
2 days ago












Perfect! You might want to remove the . character in the puts.key statement
– Archana K
2 days ago




Perfect! You might want to remove the . character in the puts.key statement
– Archana K
2 days ago












sure, but I can't :)
– VAD
2 days ago




sure, but I can't :)
– VAD
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203820%2fruby-grouping-data-from-csv-file-by-a-column%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff