How to convert columns to row using pandas
Question:
How to convert rows as columns for a grouped output for a data.
Input (Agent
and order_date_week
are the index):
Average_Rating_Per_Week
Agent order_date_week
Abi 26 4.5
27 5
28 3.8
29 4.0
30 3.9
Output should be like this:
Agent 26 27 28 29 30
Abi 4.5 5 3.8 4.0 3.9
like this I have 60 agents
Answers:
You need to select the column and unstack
:
df9['Average_Rating_Per_Week'].unstack('order_date_week')
How to convert rows as columns for a grouped output for a data.
Input (Agent
and order_date_week
are the index):
Average_Rating_Per_Week
Agent order_date_week
Abi 26 4.5
27 5
28 3.8
29 4.0
30 3.9
Output should be like this:
Agent 26 27 28 29 30
Abi 4.5 5 3.8 4.0 3.9
like this I have 60 agents
You need to select the column and unstack
:
df9['Average_Rating_Per_Week'].unstack('order_date_week')