我有一个具有多层列的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:

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?

推荐答案

MultiIndex构造函数不可用时,很难提供帮助.您可以使用stack和一些索引方法reshape 数据帧:

>>> (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

最小工作示例:

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]

Python相关问答推荐

为什么基于条件的过滤会导致pandas中的空数据框架?

如果AST请求默认受csref保护,那么在Django中使用@ system_decorator(csref_protect)的目的是什么?

更改Seaborn条形图中的x轴日期时间限制

从今天起的future 12个月内使用Python迭代

Chatgpt API不断返回错误:404未能从API获取响应

比较两个二元组列表,NP.isin

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

在Python中对分层父/子列表进行排序

使用@ guardlasses. guardlass和注释的Python继承

基于索引值的Pandas DataFrame条件填充

如何使用Python以编程方式判断和检索Angular网站的动态内容?

我想一列Panadas的Rashrame,这是一个URL,我保存为CSV,可以直接点击

所有列的滚动标准差,忽略NaN

名为__main__. py的Python模块在导入时不运行'

在输入行运行时停止代码

剪切间隔以添加特定日期

为什么'if x is None:pass'比'x is None'单独使用更快?

我对这个简单的异步者的例子有什么错误的理解吗?

如何合并具有相同元素的 torch 矩阵的行?

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&