如何在 Django 中对数据库表的各个单元格中的值进行加法和减法?

问题描述 投票:0回答:1

此图是用excel制作的数据库表的可视化示例。

如何使单元格 G6 等于 G5 和 C6 之和,然后从中减去 D6 和 E6? G7、G8 的每个下级单元都应该有相同的方案。我使用 Django Aggregation 对每列的值求和,但我无法处理添加和减去单个单元格。

python django django-models django-views django-orm
1个回答
0
投票

您应该能够像下面这样进行操作。

首先你可以使用pandas库来操作excel表格。

import pandas as pd

你可以阅读你的Excel文件:

file_path = 'path_to_your_excel_file.xlsx' # Update with your file path
df = pd.read_excel(file_path)

您可以迭代 len(df) => 您拥有的行数并进行记录计算:

# Assuming 'C', 'D', 'E', 'G' are the column headers as shown in the picture.
for i in range(5, len(df)): # Starting from row index 5 (6th row in Excel)
    df.loc[i, 'G'] = df.loc[i-1, 'G'] + df.loc[i, 'C'] - df.loc[i, 'D'] - df.loc[i, 'E']


并将更改保存为 Excel 文件:

output_file_path = 'path_to_output_excel_file.xlsx' # Update with your desired output file path
df.to_excel(output_file_path, index=False)
© www.soinside.com 2019 - 2024. All rights reserved.