重新组织数据帧

2024-05-21 05:04:17 发布

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

数据帧中有以下列:

    | invoice_number | client  | tax_rate_1_isp | tax_base_1_isp | tax_1_isp | tax_rate_2_isp | tax_base_2_isp | tax_2_isp | tax_rate_1_no_isp | tax_base_1_no_isp | tax_1_no_isp | tax_rate_2_no_isp | tax_base_2_no_isp | tax_2_no_isp | status  |
    |----------------|---------|----------------|----------------|-----------|----------------|----------------|-----------|-------------------|-------------------|--------------|-------------------|-------------------|--------------|---------|
    | #1             | client1 | 15%            |            100 |        15 |                |                |           | 0%                |               100 |            0 | 10%               |               200 |           20 | correct |
    | #2             | client2 | 0%             |            300 |         0 |                |                |           | 10%               |               100 |           10 |                   |                   |              | correct |

我想重新组织数据帧,使其看起来像这样:

invoice_number   client     tax_type   tax_rate   tax_base   tax   status
      #1        client1      isp          15%       100      15   correct
      #1        client1      no_isp       0%        100      0    correct 
      #1        client1      no_isp       10%       200      20   correct 
      #2        client2      isp          0%        300      0    correct 
      #2        client2      no_isp       10%       100      10   correct 

其中,为tax_ratetax_basetax的每组创建新行,为其余列保持相同的信息,并创建一个新列,该列将指定taxispno_isp)对应的类型,该类型在第一个数据帧的列名中标识。你知道吗

这样做的目的是最终能够从数据创建透视表。你知道吗

有没有有效的方法?你知道吗

我现在要做的是创建不同的DataFrame,选择对应于同一税收组的列,过滤那些DataFrame,只选择包含数据的行,并将它们附加到具有我需要的结构的DataFrame。你知道吗

我分享的是一个例子,然而实际数据很容易有超过50个税务组。。。你知道吗


Tags: 数据noclient类型numberdataframebaserate