将矩阵表中的数据带到按D分组的行中

2024-10-01 07:30:56 发布

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

我有一个数据集,看起来像这样,基本上是10,11和12的利润

Item            10/11             11/11            12/11
    A                30               12                 10
    B                10                5                 15
    C                5                25                 10
    D                15               10                 18

另一个数据帧:

Date       Item        A.unit       B.Unit    C.Unit      D.Unit   
10/11       A,D          5            0         0          12
11/11       A,B,C       10            10        5          0
12/11       A           20             0        0           0  

表2中销售的单位可以是任何值 现在我想要表1中A、B、C和D的计划利润列,所以输出应该是这样的。你知道吗

Date       Item        A.unit    A.Profit   B.Unit  B.Profit  C.Unit     C.Profit   D.Unit      D.Profit 
10/11       A,D          5          30          0     10         0         5           12            15
11/11       A,B,C       10           12         10     5         5         25           0            10
12/11       A           20          10           0     15        0         10            0           18

有谁能帮我把这两张表的数据放到最后一张表里吗。你知道吗


Tags: 数据dateunit单位item计划利润profit
3条回答

源数据

dd1 = {'10/11': {'A': 30, 'B': 10, 'C': 5, 'D': 15},
       '11/11': {'A': 12, 'B': 5, 'C': 25, 'D': 10},
       '12/11': {'A': 10, 'B': 15, 'C': 10, 'D': 18}}

dd2 = {'Item': {'10/11': 'A,D', '11/11': 'A,B,C', '12/11': 'A'},
       'A.unit': {'10/11': 5, '11/11': 10, '12/11': 20},
       'B.Unit': {'10/11': 0, '11/11': 10, '12/11': 0},
       'C.Unit': {'10/11': 0, '11/11': 5, '12/11': 0},
       'D.Unit': {'10/11': 12, '11/11': 0, '12/11': 0}}

df1 = pd.DataFrame.from_dict(dd1)
df2 = pd.DataFrame.from_dict(dd2)

df1
Out[1]:

    10/11   11/11   12/11
 A    30    12      10
 B    10    5       15
 C    5     25      10
 D    15    10      18


df2
Out[2]:

        Item    A.unit  B.Unit  C.Unit  D.Unit
  10/11 A,D        5    0       0       12
  11/11 A,B,C     10    10      5       0
  12/11 A         20    0       0       0

处理数据

df2.merge(df1.T.rename(columns = {c: str(c)+'.Profit' for c in df1.T.columns}), left_index = True, right_index = True)

Out[3]:

           Item   A.unit    B.Unit  C.Unit  D.Unit  A.Profit    B.Profit    C.Profit    D.Profit
Date                                    
10/11   A,D     5       0       0       12       30         10          5           15
11/11   A,B,C   10      10      5       0        12         5           25          10
12/11   A       20      0       0       0        10         15          10          18

如果第一个df1中的Item不是索引,第二个Date不是索引,则解决方法:

print (df1.index)
RangeIndex(start=0, stop=4, step=1)

print (df2.index)
RangeIndex(start=0, stop=3, step=1)

Item创建索引,首先转置和^{},然后^{},最后按.之前第三列的值排序:

df11 = df1.set_index('Item').T.add_suffix('.Profit')
df = df2.merge(df11, left_on='Date', right_index=True).reset_index()

cols = sorted(df.columns[2:], key=lambda x: x.split('.')[0])
df = df[df.columns[:2].tolist() + cols]
print (df)
    Date   Item  A.unit  A.Profit  B.Unit  B.Profit  C.Unit  C.Profit  D.Unit  \
0  10/11    A,D       5        30       0        10       0         5      12   
1  11/11  A,B,C      10        12      10         5       5        25       0   
2  12/11      A      20        10       0        15       0        10       0   

   D.Profit  
0        15  
1        10  
2        18  

如果第一列是索引:

print (df1.index)
Index(['A', 'B', 'C', 'D'], dtype='object', name='Item')

print (df2.index)
Index(['10/11', '11/11', '12/11'], dtype='object', name='Date')

df11 = df1.T.add_suffix('.Profit')
df = df2.merge(df11, left_index=True, right_index=True).reset_index()

cols = sorted(df.columns[2:], key=lambda x: x.split('.')[0])
df = df[df.columns[:2].tolist() + cols]
newdf = pd.concat([df1.transpose(), df2], axis=1)

相关问题 更多 >