From Wide to Long

Tabelo lahko enostavno pretvoriš iz excelove tabele v Markdown z:
https://thisdavej.com/copy-table-in-excel-and-paste-as-a-markdown-table/

WIDE Tabela:

ID TimeStamp A B C
1 12.05.2019 11:34 0 3 5
2 12.05.2019 12:34 1 1 3
3 12.05.2019 13:34 0 0 3

LONG Tabela:

ID TimeStamp Atribut Vrednost
1 12.05.2019 11:34 A 0
1 12.05.2019 11:34 B 3
1 12.05.2019 11:34 C 5
2 12.05.2019 12:34 A 1
2 12.05.2019 12:34 B 1
2 12.05.2019 12:34 C 3
3 12.05.2019 13:34 A 0
3 12.05.2019 13:34 B 0
3 12.05.2019 13:34 C 3

Python verzija predvorbe iz Wide v Long

In [1]:
#podatke so kopiram kar iz excelove tabele
import pandas as pd
df_w = pd.read_clipboard()
print(df_w)
   ID         TimeStamp  A  B  C
0   1  12.05.2019 11:34  0  3  5
1   2  12.05.2019 12:34  1  1  3
2   3  12.05.2019 13:34  0  0  3
In [12]:
#lahko bi naredil df_w kar tukaj:
df_w = pd.DataFrame({
    'ID': [1, 2, 3],
    'TimeStamp': ['12.05.2019 11:34', '12.05.2019 12:34', '12.05.2019 13:34'],
    'A': [0, 1, 0],
    'B': [3, 1, 0],
    'C': [5, 3, 3]
})
print(pdf_w)
   ID         TimeStamp  A  B  C
0   1  12.05.2019 11:34  0  3  5
1   2  12.05.2019 12:34  1  1  3
2   3  12.05.2019 13:34  0  0  3
In [13]:
df_w = df_w.set_index(['ID', 'TimeStamp'])
print(df_w)
                     A  B  C
ID TimeStamp                
1  12.05.2019 11:34  0  3  5
2  12.05.2019 12:34  1  1  3
3  12.05.2019 13:34  0  0  3
In [14]:
df_w = df_w.reset_index()
print(df_w)
   ID         TimeStamp  A  B  C
0   1  12.05.2019 11:34  0  3  5
1   2  12.05.2019 12:34  1  1  3
2   3  12.05.2019 13:34  0  0  3
In [16]:
#TO JE PRETVORBA iz WIDE v LONG
df_l = pd.melt(df_w, id_vars = ['ID', 'TimeStamp'])   # pd.melt(df_w, id_vars = ['ID', 'TimeStamp'], value_vars = ['A','B','C'])
print(df_l)
   ID         TimeStamp variable  value
0   1  12.05.2019 11:34        A      0
1   2  12.05.2019 12:34        A      1
2   3  12.05.2019 13:34        A      0
3   1  12.05.2019 11:34        B      3
4   2  12.05.2019 12:34        B      1
5   3  12.05.2019 13:34        B      0
6   1  12.05.2019 11:34        C      5
7   2  12.05.2019 12:34        C      3
8   3  12.05.2019 13:34        C      3

nazaj v excel kopiram z:

In [18]:
df_l.to_clipboard(excel=True)
In [27]:
#Za lepsi prikaz
from IPython.display import display, HTML
HTML(df_l.to_html())
Out[27]:
ID TimeStamp variable value
0 1 12.05.2019 11:34 A 0
1 2 12.05.2019 12:34 A 1
2 3 12.05.2019 13:34 A 0
3 1 12.05.2019 11:34 B 3
4 2 12.05.2019 12:34 B 1
5 3 12.05.2019 13:34 B 0
6 1 12.05.2019 11:34 C 5
7 2 12.05.2019 12:34 C 3
8 3 12.05.2019 13:34 C 3