Pandas DataFrame不会重新索引和转置,返回NaN

时间:2016-03-08 09:14:45

标签: python pandas

我正在读取.csv中的前9行到DataFrame,它正常工作:

invoice_desc = pd.read_csv('path', sep=',', nrows = 9, header=None)

打印时,DataFrame看起来像这样:

                  0                              1
0          Bill to                        /client/
1        Billing ID            xxxx-xxxx-xxxx-xxxx
2    Invoice number                     3359680287
3        Issue date                    31-Jan-2016
4          Due Date                    01-Mar-2016
5          Currency                           CURR
6  Invoice subtotal                   9,999,999.90
7          VAT (0%)                           0.00
8        Amount due                   9,999,999.90

我现在需要选择某些行,重新索引和转置,以便我可以通过to_sql()将其插入到MySQL数据库中:

i = ['invoiceNum', 'issueDate', 'dueDate', 'invoiceSubtotal']
invoice_desc2 = pd.DataFrame(invoice_desc.loc[[2, 3, 4, 8],], index = i)
invoice_desc2.transpose()

print invoice_desc2

但是,这段代码会重新编制索引但不保留值并在打印时生成此输出:

                   0    1
invoiceNum       NaN  NaN
issueDate        NaN  NaN
dueDate          NaN  NaN
invoiceSubtotal  NaN  NaN

我一直在阅读有关Pandas索引和切片here的内容,但我无法让它发挥作用。我究竟做错了什么?谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您可以先按loc选择i的子集,然后按T进行转置,然后按DataFrame更改列。无需按pd.DataFrame创建新print invoice_desc 0 1 0 Bill to \tclient 1 Billing ID xxxx-xxxx-xxxx-xxxx 2 Invoice number 3359680287 3 Issue date 31-Jan-2016 4 Due Date 01-Mar-2016 5 Currency CURR 6 Invoice subtotal 9,999,999.90 7 VAT (0%) 0.00 8 Amount due 9,999,999.90 invoice_desc2 = invoice_desc.loc[[2, 3, 4, 8],:] invoice_desc2 = invoice_desc2.T print invoice_desc2 2 3 4 8 0 Invoice number Issue date Due Date Amount due 1 3359680287 31-Jan-2016 01-Mar-2016 9,999,999.90 i = ['invoiceNum', 'issueDate', 'dueDate', 'invoiceSubtotal'] invoice_desc2.columns = i print invoice_desc2 invoiceNum issueDate dueDate invoiceSubtotal 0 Invoice number Issue date Due Date Amount due 1 3359680287 31-Jan-2016 01-Mar-2016 9,999,999.90

index

或首先按i设置print invoice_desc 0 1 0 Bill to \tclient 1 Billing ID xxxx-xxxx-xxxx-xxxx 2 Invoice number 3359680287 3 Issue date 31-Jan-2016 4 Due Date 01-Mar-2016 5 Currency CURR 6 Invoice subtotal 9,999,999.90 7 VAT (0%) 0.00 8 Amount due 9,999,999.90 invoice_desc2 = invoice_desc.loc[[2, 3, 4, 8],:] i = ['invoiceNum', 'issueDate', 'dueDate', 'invoiceSubtotal'] invoice_desc2.index = i print invoice_desc2 0 1 invoiceNum Invoice number 3359680287 issueDate Issue date 31-Jan-2016 dueDate Due Date 01-Mar-2016 invoiceSubtotal Amount due 9,999,999.90 print invoice_desc2.T invoiceNum issueDate dueDate invoiceSubtotal 0 Invoice number Issue date Due Date Amount due 1 3359680287 31-Jan-2016 01-Mar-2016 9,999,999.90 ,然后转置:

SELECT LAST_INSERT_ID();