我在DataFrame中有以下几列:
| 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 |
我想重新组织DataFrame,使其看起来像这样:
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_rate
,tax_base
和tax
组创建新行,其余各列保持相同的信息,并创建一个新列以指定哪种类型tax
(isp
或no_isp
)中的对应,在第一个DataFrame的列名中标识。
这样做的目的是最终能够从数据创建数据透视表。
有没有一种有效的方法?
我现在正在做的,很痛苦的事情是,创建不同的DataFrame,选择与同一税组相对应的列,过滤这些DataFrame以选择包含数据的行,并将其追加到具有以下结构的DataFrame中:我需要。
我分享的只是一个例子,但是实际数据可以轻松地包含50多个税项...