在 MATLAB 中,我創建的回圈如下所示:
header_names = {'InvoiceNo','Customer',...}
for i = 1:length(x)
entry(index i,:) = [InvoiceNo, Customer,...]
end
% Create a table from the data.
fin_table = cell2table(entry,'VariableNames',header_names);
% Write to the finish file.
writetable(fin_table,finish);
使用表值和標題,我最終會得到如下所示的內容:
| 發票號碼 | 顧客 |
|---|---|
| 1000 | 吉米 |
| 1001 | 鮑勃 |
| 1002 | 最大限度 |
| 1003 | 四月 |
| 1004 | 湯姆 |
| ... | ... |
| ... | ... |
| ... | ... |
| ... | ... |
我想知道如何在 Python 中完成此操作。我的主要問題是如何創建條目?如何將表放入 for 回圈并要求它在每次迭代的下一行列印資訊?
在 Python 中,我目前有以下內容:
for i in range(len(item)):
entry = pd.DataFrame(
[InvoiceNo, Customer, invoice, due, terms, Location, memo, item[i], item[i], quan[i], rate[i], taxable,
tax_rate, invoice, email, Class],
columns=['InvoiceNo', 'Customer', 'InvoiceDate', 'DueDate', 'Terms', 'Location', 'Memo', 'Item',
'ItemDescription', 'ItemQuantity', 'ItemRate', 'ItemAmount', 'Taxable', 'TaxRate',
'ServiceDate', 'Email', 'Class'])
# Increment the index for entry values to be correct.
index = len(item)
任何幫助都是極好的!
uj5u.com熱心網友回復:
雖然我沒有完全理解你的問題,但我會嘗試給你一些可能有用的工具:
要獲取可以使用的輸入值(并根據要創建的行數將其放入“for”回圈中)
new_InvoiceNo= input("Enter InvoiceNo:\n")
new_Customer= input("Enter Customer:\n")
new_invoice = input("Enter invoice:\n")
...
那么您可以將這些值作為串列附加到主 DF 中:
to_append = [new_InvoiceNo, new_Customer, new_invoice, ...]
new_values = pd.Series(to_append, index = df.columns)
df = df.append(new_values , ignore_index=True)
或者,您可以使用“.loc”方法:
to_append = [new_InvoiceNo, new_Customer, new_invoice, ...]
df_length = len(df)
df.loc[df_length] = to_append
嘗試在您的代碼中實作這一點并在此處報告。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357491.html
