简体   繁体   中英

Pandas DataFrame won't reindex and transpose, returns NaN

I am reading the first 9 lines from a .csv into a DataFrame, which works properly:

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

When printed, DataFrame looks like so:

                  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

I now need to pick out certain lines, re-index and transpose so I can insert this into a MySQL db via to_sql():

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

print invoice_desc2

However, that piece of code does re-index but doesn't preserve values and produces this output when printed:

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

I've been reading about Pandas indexing and slicing here but I just can't get it to work. What am I doing wrong? Thanks!

I think you can first select subset of invoice_desc by loc , transpose it by T and then change columns by i . Creating new DataFrame by pd.DataFrame is not necessary.

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

Or first set index by i and then transpose:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM