[英]How can I transform rows into repeated column based data?
我正在嘗試采用如下所示的數據集:
並將記錄轉換為以下格式:
生成的格式有兩列,一列用於舊列名,另一列用於值。 如果有10,000行,那么新格式應該有10,000組數據。
我對所有不同的方法開放,excel公式,sql(mysql),或者直接ruby代碼對我也有用。 解決這個問題的最佳方法是什么?
您可以在數據的左側添加ID列,並使用Reverse PivotTable方法。
按Alt + D + P以使用以下步驟訪問數據透視向導 :
1. Multiple Consolidation Ranges 2a. I will create the page fields 2b. Range: eg. sheet1!A1:A4 How Many Page Fields: 0 3. Existing Worksheet: H1
在數據透視表中:
Uncheck Row and Column from the Field List Double-Click the Grand Total as shown
純娛樂:
# Input file format is tab separated values
# name search_term address code
# Jim jim jim_address 123
# Bob bob bob_address 124
# Lisa lisa lisa_address 126
# Mona mona mona_address 129
infile = File.open("inputfile.tsv")
headers = infile.readline.strip.split("\t")
puts headers.inspect
of = File.new("outputfile.tsv","w")
infile.each_line do |line|
row = line.split("\t")
headers.each_with_index do |key, index|
of.puts "#{key}\t#{row[index]}"
end
end
of.close
# A nicer way, on my machine it does 1.6M rows in about 17 sec
File.open("inputfile.tsv") do | in_file |
headers = in_file.readline.strip.split("\t")
File.open("outputfile.tsv","w") do | out_file |
in_file.each_line do | line |
row = line.split("\t")
headers.each_with_index do | key, index |
out_file << key << "\t" << row[index]
end
end
end
end
destination = File.open(dir, 'a') do |d| #choose the destination file and open it
source = File.open(dir , 'r+') do |s| #choose the source file and open it
headers = s.readline.strip.split("\t") #grab the first row of the source file to use as headers
s.each do |line| #interate over each line from the source
currentLine = line.strip.split("\t") #create an array from the current line
count = 0 #track the count of each array index
currentLine.each do |c| #iterate over each cell of the currentline
finalNewLine = '"' + "#{headers[count]}" + '"' + "\t" + '"' + "#{currentLine[count]}" + '"' + "\n" #build each new line as one big string
d.write(finalNewLine) #write final line to the destination file.
count += 1 #increment the count to work on the next cell in the line
end
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.