gpt4 book ai didi

How to Transform a Pandas DataFrame with Multi-Level Columns into a Single-Level Column DataFrame in Python?(如何在Python中将具有多级列的Pandas DataFrame转换为单级列DataFrame?)

转载 作者:bug小助手 更新时间:2023-10-25 16:40:27 31 4
gpt4 key购买 nike



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


enter image description here
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


enter image description here
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

说得对,谢谢

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com