0

I have a data frame in the below format:

Date        Id       A         B         C          D        E
2018-01-28 5937.0 11.000000 11.000000 10.000000 10.000000 10.000000

2018-01-21 5937.0 10.000000 10.000000 10.000000 10.000000 10.000000

I want to change the data into the below format:

             Id       2018-01-28         2018-01-21
A           5937.0   11.000000          10.000000
B           5937.0   11.000000          10.000000
C           5937.0   10.000000          10.000000
D           5937.0   10.000000          10.000000
E           5937.0   10.000000          10.000000

What is the best method to carry out following transformation. I have been using pivot but its not working(I am not very good with pivot)

1

3 Answers 3

2

Use set_index followed by stack and unstack with reset_index:

df1 = df.set_index(['Date','Id']).stack().unstack(0).reset_index(0)

print(df1)
Date      Id  2018-01-21  2018-01-28
A     5937.0        10.0        11.0
B     5937.0        10.0        11.0
C     5937.0        10.0        10.0
D     5937.0        10.0        10.0
E     5937.0        10.0        10.0

df1=df.set_index(['Date','Id']).stack().unstack(0).reset_index(0).rename_axis(None,1)

print(df1)
       Id  2018-01-21  2018-01-28
A  5937.0        10.0        11.0
B  5937.0        10.0        11.0
C  5937.0        10.0        10.0
D  5937.0        10.0        10.0
E  5937.0        10.0        10.0
Sign up to request clarification or add additional context in comments.

Comments

1

I would do this using melt and pivot_table:

(df.melt(['Date', 'Id'])
   .pivot_table(index=['variable', 'Id'], columns='Date', values='value')
   .reset_index())


Date variable      Id  2018-01-21  2018-01-28
0           A  5937.0        10.0        11.0
1           B  5937.0        10.0        11.0
2           C  5937.0        10.0        10.0
3           D  5937.0        10.0        10.0
4           E  5937.0        10.0        10.0

Comments

1

Using pivot:

(df.pivot_table(values=["A", "B", "C", "D", "E"], columns=["Id", "Date"])
    .unstack()
    .reset_index(1) # Multi-index level 1 = Id
    .rename_axis(None, 1)) # Set columns name to None (not Date)

Output:

Date      Id  2018-01-21  2018-01-28
A     5937.0        10.0        11.0
B     5937.0        10.0        11.0
C     5937.0        10.0        10.0
D     5937.0        10.0        10.0
E     5937.0        10.0        10.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.