【发布时间】:2021-12-27 02:38:04
【问题描述】:
在 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);
使用表值和标题,我最终会得到如下所示的内容:
| InvoiceNo | Customer |
|---|---|
| 1000 | Jimmy |
| 1001 | Bob |
| 1002 | Max |
| 1003 | April |
| 1004 | Tom |
| ... | ... |
| ... | ... |
| ... | ... |
| ... | ... |
我想知道如何在 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)
任何帮助都会很棒!
【问题讨论】:
-
您的数据是否保存在 csv 文件中?
-
@Ted 输入数据保存在 xlsx 文件中。那是我从中获取原始信息的地方。使用更新的信息创建新表后,我将根据“条目”将新数据发送到 csv 文件。
标签: python pandas for-loop indexing