在Ruby中将数据从一列转换为多列

时间:2012-05-22 07:57:57

标签: ruby

我有两列csv格式的数据,如下所示,来自预测服务器。第一列是每个预测的每个变量的索引位置。因此,新数据从索引1开始。

1,2.0
2,1.5
3,1.4
1,1.1
2,2.0
3,1.5
4,2.0
5,1.6
1,2.0
2,4.0

.
.
.

我希望以这种格式提供数据,

2.0,1.1,2.0
1.5,2.0,4.0
1.4,1.5
    2.0
    1.6

为了便于工作,空的'单元'可以用零填充或#例如

2.0,1.1,2.0
1.5,2.0,4.0
1.4,1.5,0
0,  2.0,0
0,  1.6,0

有人以优雅的方式在Ruby中执行此操作吗?

2 个答案:

答案 0 :(得分:2)

这应该适合你:

require 'csv'

# read csv contents from file to array
rows = CSV.read("path/to/in_file.csv")

res = Hash.new {|h,k| h[k] = []}
rows.each do |(key, val)|
  res[key] << val
end

# write to output csv file
CSV.open("path/to/out_file.csv", "wb") do |csv|
  # sort res hash by keys, map to have array of values and add to csv
  res.sort_by{|k, v| k}.map{|k, v| v}.each do |r|
    csv << r
  end
end

答案 1 :(得分:2)

让我们尝试使用Array#transpose:

转置它
# first get a 2d representation of the data
rows = CSV.read(fn).slice_before{|row| "1" == row[0]}.map{|x| x.map{|y| y[1]}}

# we want to transpose the array but first we have to fill empty cells
max_length = rows.max_by{|x| x.length}.length
rows.each{|row| row.fill '#', row.length..max_length}

# now we can transpose the array
pp rows.transpose

["2.0", "1.1", "2.0", "5.0"],
["1.5", "2.0", "4.0", "#"],
["1.4",  "1.5", "#", "#"],
["#", "2.0", "#", "#"],
["#", "1.6", "#", "#"], 
["#", "#", "#", "#"]
相关问题