UnPivot a Pandas Dataset with Join

How to convert this source dataset into a flattened dataset using the Python Pandas frame frame?

Initial data

Like this flattened data:

enter image description here

I tried to "fit" the data and reset the index, but this caused an undesirable result.

df = xl.parse("data")
stack = df.stack(-1).reset_index(0)

Thanks in advance for your help.

+4
source share
1 answer

You are looking for melt(aka "univot"):

In [11]: df = pd.DataFrame([["a", "b", 43, 87, 29]], columns=["N", "P", 1, 2, 3])

In [12]: pd.melt(df, id_vars=["N", "P"], value_vars=[1, 2, 3], var_name="Day")
Out[12]:
   N  P Day  value
0  a  b   1     43
1  a  b   2     87
2  a  b   3     29
+5
source

Source: https://habr.com/ru/post/1612851/


All Articles