how to transpose a table by a column with group by pandas

Question:

I have a table that I need to transpose, and it looks like this:


| User ID| Week | clicks | days|
|------- |------| -------|-----|
| x      | 1    | 1      | 1   | 
| x      | 2    | 2      | 3   |
| y      | 1    | 3      | 2   | 
| y      | 2    | 3      | 2   |
| y      | 3    | 4      | 3   | 
  

The output I need looks like this:

| User ID| clicks week 1| clicks week 2| clicks week 3| days week 1| days week 2| days week 3|
|--------|--------------| -------------|------------- |------------| -----------|------------|
| x      | 1            | 2            | 0            | 1          | 3          | 0          |
| y      | 3            | 3            | 4            | 2          | 2          | 3          |

For each user I need a single row and for each category I need a column for each week (if there’s no activity for user in a week, write 0).

Any idea how to do it?

Thanks!

Asked By: kri

||

Answers:

You can use .pivot() with .fillna():

df.pivot(index="User ID", columns="Week", values=["clicks", "days"]).fillna(0)

This outputs:

          clicks                    days
Week       1        2        3     1     2     3
User ID
x        1        2              0  1     3        0
y        3        3        4        2     2     3
Answered By: BrokenBenchmark

You can try pandas pivot method:

testing.pivot(index="User ID",columns=["Week"],values=["clicks","days"]).fillna(0)

         clicks         days
Week    1   2   3   1   2   3
User ID                     
x       1.0 2.0 0.0 1.0 3.0 0.0
y       3.0 3.0 4.0 2.0 2.0 3.0
Answered By: Adnan Shaikh

You can do it multiple ways.
no 1. You can use .pivot and assign index='User ID', columns='Week', values=['clicks', "days"]

df.pivot(index='User ID', columns='Week', values=['clicks', "days"])

no 2. You can use groupby + unstack, if aggregations is required. groupby method is used to group the data by columns, and the unstack is used to pivot the data.

df.groupby(['User ID', 'Week'], sort=False).sum().unstack()

No 3 Another solution is using set_index + unstack

df.set_index(['User ID', 'Week']).unstack()
Answered By: Shounak Das
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.