How do I transform this starting data set into a flattened data set with a Python Pandas data frame?
Like this flattened data:
I tried to "stack" the data and reset the index, but this produced an undesired result.
df = xl.parse("data")
stack = df.stack(-1).reset_index(0)
Thanks in advance for your help.
You're looking for melt
(aka "unpivot"):
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.