I have a Pandas DataFrame with multi-level columns like the one below:
我有一个具有多层列的Pandas DataFrame,如下所示:
|
|
|
|
23-Jan |
|
|
|
|
|
|
|
23-Feb |
|
|
|
|
|
|
|
Market |
Product |
City |
Territory |
VALUES |
Values MARKET SHARE |
VALUES GROWTH |
VALUES GEO. SHARE |
UNITS |
UNITS MARKET SHARE |
UNITS GROWTH |
UNITS GEO. SHARE |
VALUES |
Values MARKET SHARE |
VALUES GROWTH |
VALUES GEO. SHARE |
UNITS |
UNITS MARKET SHARE |
UNITS GROWTH |
UNITS GEO. SHARE |
I want to create a Python function that transforms this DataFrame into the following format:
我想创建一个Python函数,将此DataFrame转换为以下格式:
Market |
Product |
City |
Territory |
VALUES |
Values MARKET SHARE |
VALUES GROWTH |
VALUES GEO. SHARE |
UNITS |
UNITS MARKET SHARE |
UNITS GROWTH |
UNITS GEO. SHARE |
Date |
|
|
|
|
|
|
|
|
|
|
|
|
23-Jan |
|
|
|
|
|
|
|
|
|
|
|
|
23-Feb |
How can I achieve this transformation using Python and Pandas?
我如何使用Python和Pandas实现这种转换?
更多回答
优秀答案推荐
It's difficult to help when the MultiIndex
constructor is not available. You can reshape dataframe using stack
and some index methods:
当多索引构造函数不可用时,很难提供帮助。您可以使用堆栈和一些索引方法重塑数据帧:
>>> (df.set_index(df.columns[:4].tolist()) # Market, Product, City, Territory
.rename_axis(index=df.columns[:4].droplevel(0), # Flat them
columns=['Date', None]) # Define column names
.stack('Date', sort=False).reset_index()) # Reshape your dataframe
Market Product City Territory Date VALUES Values MARKET SHARE VALUES GROWTH VALUES GEO. SHARE UNITS UNITS MARKET SHARE UNITS GROWTH UNITS GEO. SHARE
0 0 0 0 0 23-Jan 1 1 1 1 1 1 1 1
1 0 0 0 0 23-Feb 2 2 2 2 2 2 2 2
Minimal Working Example:
最小工作示例:
data = {('', 'Market'): {0: 0},
('', 'Product'): {0: 0},
('', 'City'): {0: 0},
('', 'Territory'): {0: 0},
('23-Jan', 'VALUES'): {0: 1},
('23-Jan', 'Values MARKET SHARE'): {0: 1},
('23-Jan', 'VALUES GROWTH'): {0: 1},
('23-Jan', 'VALUES GEO. SHARE'): {0: 1},
('23-Jan', 'UNITS'): {0: 1},
('23-Jan', 'UNITS MARKET SHARE'): {0: 1},
('23-Jan', 'UNITS GROWTH'): {0: 1},
('23-Jan', 'UNITS GEO. SHARE'): {0: 1},
('23-Feb', 'VALUES'): {0: 2},
('23-Feb', 'Values MARKET SHARE'): {0: 2},
('23-Feb', 'VALUES GROWTH'): {0: 2},
('23-Feb', 'VALUES GEO. SHARE'): {0: 2},
('23-Feb', 'UNITS'): {0: 2},
('23-Feb', 'UNITS MARKET SHARE'): {0: 2},
('23-Feb', 'UNITS GROWTH'): {0: 2},
('23-Feb', 'UNITS GEO. SHARE'): {0: 2}}
df = pd.DataFrame(data)
print(df)
# Output
23-Jan ... 23-Feb
Market Product City Territory VALUES Values MARKET SHARE ... VALUES GROWTH VALUES GEO. SHARE UNITS UNITS MARKET SHARE UNITS GROWTH UNITS GEO. SHARE
0 0 0 0 0 1 1 ... 2 2 2 2 2 2
[1 rows x 20 columns]
更多回答
That got it right, Thank you
说得对,谢谢
我是一名优秀的程序员,十分优秀!