How to take advantage of python pandas library to transform my data as follows?

Question:

I have the following data:

enter image description here

I want to transform it as follows:

enter image description here

How to take advantage of python pandas library to do this?

Asked By: Qendrim Krasniqi

||

Answers:

you achieve this using the melt command:

df.melt(id_vars=['ID'], value_vars=['2020-01-01', '2020-01-02', '2020-01-03'], var_name='DATE', value_name='AMOUNT', ignore_index=False)

result:

ID        DATE  AMOUNT
10  2020-01-01    11.0
11  2020-01-01     0.5
12  2020-01-01    15.0
10  2020-01-02    12.0
11  2020-01-02     0.0
12  2020-01-02    15.0
10  2020-01-03   101.0
11  2020-01-03     0.6
12  2020-01-03    15.0
Answered By: jt_sobrinho
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.