Let's assume you have a pandas DataFrame that holds frequency information like this:
data = [[1,1,2,3],
[1,2,3,5],
[2,1,6,1],
[2,2,2,4]]
df = pd.DataFrame(data, columns=['id', 'time', 'CountX1', 'CountX2'])
# id time CountX1 CountX2
# 0 1 1 2 3
# 1 1 2 3 5
# 2 2 1 6 1
# 3 2 2 2 4
I am looking for a simple command (e.g. using pd.pivot
or pd.melt()
) to revert these frequencies to tidy data that should look like this:
id time variable
0 1 X1
0 1 X1
0 1 X2
0 1 X2
0 1 X2
1 1 X1
1 1 X1
1 1 X1
1 1 X2 ... # 5x repeated
2 1 X1 ... # 6x repeated
2 1 X2 ... # 1x repeated
2 2 X1 ... # 2x repeated
2 2 X2 ... # 4x repeated