将数据从矩阵表带到按日期分组的行

时间:2019-11-20 07:48:11

标签: python pandas list dataframe metadata

我有一个看起来像这样的数据集,这些基本上是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

任何人都可以帮助我如何在最后一个表中获取这两个表数据。

3 个答案:

答案 0 :(得分:1)

如果前Item中的df1不是索引,而后第二Date不是索引,则解决方案:

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

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

通过Item创建索引,先进行转置和DataFrame.add_suffix,然后进行DataFrame.merge,最后按.之前的第三列的值进行排序:

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]

答案 1 :(得分:0)

newdf = pd.concat([df1.transpose(), df2], axis=1)

答案 2 :(得分:0)

源数据

public MainView() {
    Div textContainer = new Div();

    textContainer.add(createHeader("Organization Using Domain Name"));
    textContainer.add(createLine("Name", dto.getDomainInfo().getDomainName()));
    textContainer.add(createLine("Organization Name", dto.getDomainInfo().getOrganizationName()));
    textContainer.add(createLine("Street Address", dto.getDomainInfo().getStreetAddress()));

    textContainer.add(createHeader("Administrative Contact/Agent"));
    textContainer.add(createLine("Handle", dto.getDomainInfo().getHandle()));
    textContainer.add(createLine("Name", dto.getDomainInfo().getName()));

    add(textContainer);
}

private Component createLine(String key, String value) {
    int keyLength = 24;
    StringBuilder sb = new StringBuilder(key);

    // Add padding dots ...
    for(int i = key.length(); i < keyLength; i++) {
        sb.append(".");
    }
    sb.append(": ");
    sb.append(value);

    Paragraph line = new Paragraph(sb.toString());
    line.getStyle().set("font-family", "monospace");
    line.getStyle().set("margin", "0");

    return line;
}

private Component createHeader(String caption) {
    Paragraph header = new Paragraph(caption);
    header.getStyle().set("font-family", "monospace");
    header.getStyle().set("margin", "16px 0 0 0 ");
    return header;
}

处理数据

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