[英]Pivot/ Unstack a Pandas Dataframe in Python
I have the following dataframe我有以下数据框
01/01/2017 02/01/2017
Productid ProductName Sales Discount Sales Discount
1 abc 100 12 234 23
2 xyz 156 13 237 13
3 pqr 300 12 198 18
I need to convert this into the following dataframe.我需要将其转换为以下数据帧。
Productid ProductName Date Sales Discount
1 abc 01/01/2017 100 12
1 abc 02/01/2017 234 23
2 xyz 01/01/2017 156 13
2 xyz 02/01/2017 237 13
3 pqr 01/01/2017 300 12
3 pqr 02/01/2017 198 18
How can I do this in Python?我怎样才能在 Python 中做到这一点?
Multi-index are difficult to reproduce directly.多索引很难直接重现。 So first initializing the dataframe as per the OP's original dataframe.
因此,首先根据 OP 的原始数据帧初始化数据帧。
df = pd.read_clipboard() #reading part of OP's Dataframe
df
Productid ProductName Sales Discount Sales.1 Discount.1
0 1 abc 100 12 234 23
1 2 xyz 156 13 237 13
2 3 pqr 300 12 198 18
df.columns = ['Productid', 'ProductName', 'Sales', 'Discount', 'Sales', 'Discount']
df.set_index(keys=['Productid','ProductName'],inplace=True)
df
Sales Discount Sales Discount
Productid ProductName
1 abc 100 12 234 23
2 xyz 156 13 237 13
3 pqr 300 12 198 18
array = [['01/01/2017','01/01/2017','02/01/2017','02/01/2017'],
['Sales', 'Discount', 'Sales', 'Discount']]
df.columns = pd.MultiIndex.from_arrays(array) #setting multi-index
Assuming this is the OP's Dataframe:假设这是 OP 的 Dataframe:
df
01/01/2017 02/01/2017
Sales Discount Sales Discount
Productid ProductName
1 abc 100 12 234 23
2 xyz 156 13 237 13
3 pqr 300 12 198 18
Solution using stack
and level=0
parameter, then reset_index()
on level=[0,1]
and reset_index()
again.使用溶液
stack
和level=0
参数,然后reset_index()
上level=[0,1]
和reset_index()
一次。 Finally changing name of index
column to Date
using rename
:最后使用
rename
index
列的名称更改为Date
:
df = df.stack(level=0).reset_index(level=[0,1]).reset_index()
df.rename(columns={'index':'Date'},inplace=True)
df[['Productid', 'ProductName','Date','Sales','Discount']]
Productid ProductName Date Sales Discount
0 1 abc 01/01/2017 100 12
1 1 abc 02/01/2017 234 23
2 2 xyz 01/01/2017 156 13
3 2 xyz 02/01/2017 237 13
4 3 pqr 01/01/2017 300 12
5 3 pqr 02/01/2017 198 18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.