Python Forum
Normalize table (remove redundancy) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Normalize table (remove redundancy) (/thread-31545.html)



Normalize table (remove redundancy) - CatorCanulis - Dec-17-2020

Hi everyone,

I have a excel table like this:


CODE | MATRIX | SAMPLING | FREQUENCY | PARAM
XX123 | OG | spot | 14 | 99
XX123 | OG | spot | 14 | 50
XX123 | OG | spot | 14 | 1620
AB006 | OG | mix | 28 | 3
AB006 | OG | mix | 28 | 4005


Can I somehow normalize this data to a list of dictionaries like this?:
[[ {'CODE': 'XX123', 'MATRIX': 'OG', 'SAMPLING':'spot', 'FREQUENCY':14, 'PARAM':[99, 50, 1620]},
{'CODE': 'AB006', 'MATRIX': 'OG', 'SAMPLING':'mix', 'FREQUENCY':28, 'PARAM':[3, 4005]} ]
I tried googling this problem but didn't come across an exact match.

Thanks in advance
Markus


RE: Normalize table (remove redundancy) - ibreeden - Dec-20-2020

Is it possible to change the layout of the dictionary so the key is comprised of ('CODE ', ' MATRIX ', ' SAMPLING ', ' FREQUENCY '). This would make it much simpler to build up the values of the last column.
It would look like this:
Output:
{ ('CODE ', ' MATRIX ', ' SAMPLING ', ' FREQUENCY '): [' PARAM'], ('XX123 ', ' OG ', ' spot ', ' 14 '): [' 99', ' 50', ' 1620'], ('AB006 ', ' OG ', ' mix ', ' 28 '): [' 3', ' 4005'] }