将列转换为Horizo​​ntal PIVOT

时间:2017-06-07 02:32:25

标签: excel excel-vba unix awk vba

我有3个包含ID,名称和值的列文件。

,Brand,Color,Gender,Logo,width,Fits,catgegory,primarycolor,size
1,sports,White,Male,yes,10,,,,
4,Running,,,,12,Lose,,,
3,,blue/white,,,,,shoe,blue,
5,Sports/Running,,,,,,,,M

我试图根据第1列和第2列将其转换为横向格式。

它类似于打印文本值的数据透视表,它不能使用Excel数据透镜完成,它只允许值的总和/计数。

这可以在UNIX中生成吗?

interface IMyCaller {
  <R>([fn]: [() => R]): R;
  <R,A>([fn, a]: [(a: A) => R, A]): R;
  <R,A,B>([fn, a, b]: [(a: A, b: B) => R, A, B]): R;
  <R,A,B,C>([fn, a, b, c]: [(a: A, b: B, c: C) => R, A, B, C]): R;
  // keep adding these until you get tired
}

const myCaller: IMyCaller = ([fun, ...args]) => fun.apply(args);

3 个答案:

答案 0 :(得分:0)

<强>输入

Base64 Encoded Message Here. It's too long.

<强>脚本

akshay@db-3325:/tmp$ cat data.txt 
1,Brand,sports
1,Color,White
1,Gender,Male
1,Logo,yes
1,width,10
4,Brand,Running
4,width,12
4,Fits,Lose
3,catgegory,shoe
3,Color,blue
3,primarycolor,blue
5,size,M
5,Brand,Running

执行和输出

akshay@db-3325:/tmp$ cat pivot.awk 
{   
     id=$1; name=$2; value=$3
     ids[id]; 
     # this is to retain order
     if(!(name in tmp)){ tmp[name]; names[++c]=name; }
     values[id,name] = value
}
END {
     # comment below line if you hide "id"
     printf "id"

     for (name in names) {
         printf "%s%s",OFS,names[name]
     }
     print ""
     for (id in ids) {
         printf "%s",id
         for (name in names) {
             printf "%s%s",OFS,values[id,names[name]]
         } print ""
     }
}

这产生的o / p与预期的o / p相同,包括订单

akshay@db-3325:/tmp$ awk -v FS=, -v OFS=, -f pivot.awk data.txt 
id,Brand,Color,Gender,Logo,width,Fits,catgegory,primarycolor,size
1,sports,White,Male,yes,10,,,,
3,,blue,,,,,shoe,blue,
4,Running,,,,12,Lose,,,
5,Running,,,,,,,,M

<强>输出

akshay@db-3325:/tmp$ cat pivot_with_order.awk 
{   
     id=$1; name=$2; value=$3

     # this is to retain order
     if(!(id in itmp)){ itmp[id]; ids[++i]=id; } 
     if(!(name in tmp)){ tmp[name]; names[++c]=name; }

     values[id,name] = value
}
END {
     # uncomment below line if you want to display "id"
     # printf "id"

     for (name in names) {
         printf "%s%s",OFS,names[name]
     }
     print ""
     for (id in ids) {
         printf "%s",ids[id]
         for (name in names) {
             printf "%s%s",OFS,values[ids[id],names[name]]
         } print ""
     }
}

答案 1 :(得分:0)

Excel VBA:

Sub Pivot()

    Dim rngData, rngOut, r, c, dR, dC
    Set dR = CreateObject("scripting.dictionary")
    Set dC = CreateObject("scripting.dictionary")

    Set rngData = ActiveSheet.Range("A2:C2") '<< first row of input
    Set rngOut = ActiveSheet.Range("G2")     '<< where to put output

    Do While Application.CountA(rngData) > 0
        r = rngData(1)
        c = rngData(2)
        If Not dR.exists(r) Then
            dR.Add r, dR.Count + 1
            rngOut.Offset(dR.Count, 0) = r
        End If
        If Not dC.exists(c) Then
            dC.Add c, dC.Count + 1
            rngOut.Offset(0, dC.Count) = c
        End If

        With rngOut.Offset(dR(r), dC(c))
            'if already has a value, add a newline separator
            .Value = .Value & IIf(.Value <> "", vbLf, "") & rngData(3)
        End With
        Set rngData = rngData.Offset(1, 0)
    Loop

End Sub

答案 2 :(得分:0)

Python 2.7

假设list.csv是:

1,Brand,sports
1,Color,White
1,Gender,Male
1,Logo,yes
1,width,10
4,Brand,Running
4,width,12
4,Fits,Lose
3,catgegory,shoe
3,Color,blue
3,Color,white
3,primarycolor,blue
5,size,M
5,Brand,Sports
5,Brand,Running

un.py中的Python代码:

# up.py
#
import csv
rowid={};  colid={};  tab={};  rowTitle=[];  colTitle=[]
with open('list.csv', 'rb') as csvfile:
  v = csv.reader(csvfile)
  for row in v:
    rowid[row[0]]=1;  colid[row[1]]=1;  k=row[0]+"@"+row[1]
    if tab.has_key(k):
      tab[k]=tab[k]+"/"+row[2]        
    else:
      tab[k]=row[2]

rowTitle=rowid.keys(); colTitle=colid.keys()
rowTitle.sort();  colTitle.sort();

s=""
for j in colTitle: s=s+","+j 
print s
for i in rowTitle:
  s=i
  for j in colTitle:
    k=i+"@"+j
    s=s+","
    if tab.has_key(k):
      s=s+tab[k]  
  print s

py un.py输出为:

,Brand,Color,Fits,Gender,Logo,catgegory,primarycolor,size,width
1,sports,White,,Male,yes,,,,10
3,,blue/white,,,,shoe,blue,,
4,Running,,Lose,,,,,,12
5,Sports/Running,,,,,,,M,