Until you provide a Minimal, Complete, and Verifiable example, I will not test this answer but the following should work:
given that we have the data stored in a Pandas DataFrame
called df
, we can use pd.melt
moltendfs = []
for i in range(4):
moltendfs.append(df.iloc[:, i::4].reset_index().melt(id_vars='date'))
newdf = pd.concat(moltendfs, axis=1)
We use iloc
to take only every fourth column, starting with the i-th column. Then we reset_index
in order to be able to keep the date column as our identifier variable. We use melt
in order to melt our DataFrame. Finally we simply concatenate all of these molten DataFrames together side by side.