Pandas Melt
First some info on wide and long formats on Wikipedia. The subject of this post is how to switch from one format to the other.
Why is that necessary?
Well, for me it’s software requirements. For example, some Javascript libraries require that the data be in the long format. So here is how to do that. First let’s GET the table from Wikipedia
import pandas as pd
url='https://en.wikipedia.org/wiki/Wide_and_narrow_data'
t=pd.read_html(url)
t0=t[0]
t0_headers=t0.loc[0]
wide = t0.drop(0)
wide.columns=t0_headers
Wide to Long
Here is the table in wide format
Person | Age | Weight | Height | |
---|---|---|---|---|
1 | Bob | 32 | 128 | 180 |
2 | Alice | 24 | 86 | 175 |
3 | Steve | 64 | 95 | 165 |
long = pd.melt(wide,
id_vars='Person',
var_name='Variable',
value_name='Value')
Here is the same table in long format
Person | Variable | Value | |
---|---|---|---|
0 | Bob | Age | 32 |
1 | Alice | Age | 24 |
2 | Steve | Age | 64 |
3 | Bob | Weight | 128 |
4 | Alice | Weight | 86 |
5 | Steve | Weight | 95 |
6 | Bob | Height | 180 |
7 | Alice | Height | 175 |
8 | Steve | Height | 165 |
Want a JSON?
print(long.to_json(orient='records'))
The resule is below
[{"Person":"Bob","Variable":"Age","Value":"32"},{"Person":"Alice","Variable":"Age","Value":"24"},{"Person":"Steve","Variable":"Age","Value":"64"},{"Person":"Bob","Variable":"Weight","Value":"128"},{"Person":"Alice","Variable":"Weight","Value":"86"},{"Person":"Steve","Variable":"Weight","Value":"95"},{"Person":"Bob","Variable":"Height","Value":"180"},{"Person":"Alice","Variable":"Height","Value":"175"},{"Person":"Steve","Variable":"Height","Value":"165"}]
Long to Wide
Later. 😀