如何重新组织包含多个组的数据帧?

2024-04-19 05:20:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个非常大的转置数据帧,我试图按时间顺序,按项目组织。然而,它的格式非常奇怪,我很难想出如何组织作为一个整体的框架。我可以一次挑选一个项目并对其进行诊断,但这是不可伸缩的。你知道吗

以下是数据帧的示例:

0         1       2     3     4     5
0      RowLabel ItemA ItemB ItemC ItemD...
2019   Oct      -22     12   15     0
2019   Nov      -4      -8   -4     12
2020   Jan       0      0    -3     5
...

我为一个项目修改的是我想为所有项目重复的内容,基本上是:

Year   Month   Qty   Item
2019   Oct     -22   ItemA
2019   Oct      12   ItemB
2019   Oct      15   ItemC
2019   Oct      0    ItemD
2019   Nov     -4    ItemA
2019   Nov     -8    ItemB
2019   Nov     -4    ItemC
2019   Nov     12    ItemD
...

有没有一个简单的方法可以做到这一点,我错过了?你知道吗


Tags: 数据项目框架示例顺序格式时间oct
1条回答
网友
1楼 · 发布于 2024-04-19 05:20:54

根据OP评论中的澄清,以下是我认为您想要做的:

import pandas as pd

df = pd.DataFrame(
{0: [0, 2019, 2019, 2020],
 1: ['RowLabel', 'Oct', 'Nov', 'Jan'],
 2: ['ItemA', -22, -4, 0],
 3: ['ItemB', 12, -8, 0],
 4: ['ItemC', 15, -4, -3],
 5: ['ItemD', 0, 12, 5]})

# get proper column names from the first table row
df.columns = df.iloc[0].replace({0: 'Year', 'RowLabel': 'Month'})
df.drop(index=0, inplace=True)

# set index; all columns which are not the index will be stacked
df.set_index(['Year', 'Month'], inplace=True)

# give the column levels proper names, which will become the stacked table columns
df.columns.name = 'Item'

# at this point, the table is ready for stacking: the remaining columns are 
# only the ItemX columns, all others are in the index
df.head(2)

Out[1]: 
Item       ItemA ItemB ItemC ItemD
Year Month                        
2019 Oct     -22    12    15     0
     Nov      -4    -8    -4    12

# stack
df = df.stack().rename('Qty').reset_index()

df.head()

Out[2]: 
   Year Month   Item  Qty
0  2019   Oct  ItemA  -22
1  2019   Oct  ItemB   12
2  2019   Oct  ItemC   15
3  2019   Oct  ItemD    0
4  2019   Nov  ItemA   -4

相关问题 更多 >