gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:46 25 4
gpt4 key购买 nike

我正在将 .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但我就是无法让它发挥作用。我究竟做错了什么?谢谢!

最佳答案

我认为您可以首先通过 loc 选择 invoice_desc 的子集,将其转置为 T然后按 i 更改列。不需要通过 pd.DataFrame 创建新的 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

或者先通过i设置index,然后转置:

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

关于python - Pandas DataFrame 不会重新索引和转置,返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com