1

我有两列来自预测服务器的 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 中做到这一点?

4

2 回答 2

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
于 2012-05-22T08:36:26.063 回答
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", "#", "#"], 
["#", "#", "#", "#"]
于 2012-05-22T10:43:44.627 回答