python-3.x 如何计算产品明智的总和在一个dataframe和写在xlsxwriter工作表下面的小计?

q43xntqr  于 2023-05-08  发布在  Python
关注(0)|答案(2)|浏览(101)

我想用xlsxwriter计算乘积和并显示在excel表中。输入数据框如下所示:

final_fact = pd.DataFrame({'factory': ['kerala', 'kerala', 'kerala', 'delhi', 'delhi', 'goa', 'goa'],
           'plant': ['', '', '', '', '', '', ''],
           'market': ['', '', '', '', '', '', ''],
           'product': ['A', 'B', 'C', 'A', 'B', 'A', 'B'],
           'uom': ['l', 'l', 'l', 'l', 'l', 'l', 'l'],
           'BP4-2023': [4, 4, 5, 6, 4, 5, 5],
           'RE4-2023': [7, 7, 8, 8, 7, 8, 8],
           'BP5-2023': [4, 4, 5, 6, 4, 5, 5],
           'RE5-2023': [7, 7, 8, 8, 7, 8, 8]})

我想在下面的Excel工作表输出如下小计行:

Product wise Total                                 
 A                            l    15        23       15        23
 B                            l    13        22       13        22
 C                            l    5         8        5         8

值应在相应的BPRE下。
我使用下面的代码来实现这一点。直到小计部分(包括小计行),数据显示在excel工作表是好的。但其余部分是不正确的。
验证码:

factory_value = final_fact['factory'].unique()
                total_dataframe = pd.DataFrame(columns=final_fact.columns)
                total = []
                for fact_val in factory_value:
                    factory_data = final_fact[final_fact['factory'] == fact_val]
                    factory_data = factory_data.sort_values(by=['product'], ascending=True)
                    factory_data.loc['plant_total'] = factory_data.select_dtypes(include=['float64']).sum()
                    factory_data["product"] = factory_data["product"].replace(np.nan, "Plant Total")
                    factory_data = factory_data.fillna('')
                    factory_data = factory_data.round(2)

                    plant_total_row = factory_data.loc[factory_data['product'] == "Plant Total"]
                    total.append(plant_total_row)
                    output = factory_data.values.tolist()

                    row_num += 1
                    for data_item in output:
                        plant_total = "Plant Total"
                        if plant_total in data_item:
                            for col_num in range(len(data_item)):
                                worksheet.write(row_num, col_num, data_item[col_num], header_right_format)
                            row_num += 1
                        else:
                            for col_num in range(len(data_item)):
                                worksheet.write(row_num, col_num, data_item[col_num])
                            row_num += 1

                for i in total:
                    total_dataframe = pd.concat([total_dataframe, i], ignore_index=True)
                ignore = ['factory', 'planttype', 'market', 'product', 'uom']
                total_dataframe = (total_dataframe.set_index(ignore, append=True).astype(float).reset_index(ignore))
                total_dataframe.loc['sub_total'] = total_dataframe.select_dtypes(include=['float64']).sum()
                total_dataframe["product"] = total_dataframe["product"].replace(np.nan, "Sub Total")
                total_dataframe = total_dataframe.fillna('')
                total_dataframe = total_dataframe.round(2)
                print(total_dataframe)
                output = total_dataframe.values.tolist()

                for data_item in output:
                    total_production = "Sub Total"
                    if total_production in data_item:
                        for col_num in range(len(data_item)):
                            worksheet.write(row_num, col_num, data_item[col_num], header_right_format)
                        row_num += 1

                product_wise_total = final_fact.groupby(['product', 'uom']).sum()
                product_wise_output = product_wise_total.values.tolist()
                print(product_wise_total)

                for product_data_item in product_wise_output:
                    for product_wise_col_num in range(len(product_data_item)):
                        worksheet.write(row_num, product_wise_col_num, product_data_item[product_wise_col_num])
                        row_num += 1

有人能提出解决这个问题的方法吗?

nvbavucw

nvbavucw1#

如果我理解正确的话,你想计算"product"列中每个字母的值的总和
如果是这样,你可以简单地使用pandasdocs)的groupby函数:

final_fact.groupby(by='product').sum()

那么结果是:

product BP4-2023    RE4-2023    BP5-2023    RE5-2023
A       15          23          15          23
B       13          22          13          22
C       5           8           5           8
dkqlctbz

dkqlctbz2#

我认为你可以创建所有必要的DataFrames,然后通过multiple_dfs写入Excel:

total_dataframe = (final_fact.groupby('factory')
                             .sum()
                             .assign(plant='Plant Total')
                             .set_index('plant', append=True)
                             .reset_index())
print(total_dataframe)
  factory        plant  BP4-2023  RE4-2023  BP5-2023  RE5-2023
0   delhi  Plant Total        10        15        10        15
1     goa  Plant Total        10        16        10        16
2  kerala  Plant Total        13        22        13        22 

product_wise_total = (final_fact.groupby(['product', 'uom'])
                                .sum()
                                .assign(plant='Sub Total')
                                .set_index('plant', append=True)
                                .reset_index())
print (product_wise_total)
  product uom      plant  BP4-2023  RE4-2023  BP5-2023  RE5-2023
0       A   l  Sub Total        15        23        15        23
1       B   l  Sub Total        13        22        13        22
2       C   l  Sub Total         5         8         5         8
# funtion
#https://stackoverflow.com/a/33004253/2901002
def multiple_dfs(df_list, sheets, file_name, spaces):
    writer = pd.ExcelWriter(file_name,engine='xlsxwriter')   
    row = 0
    for dataframe in df_list:
        dataframe.to_excel(writer,sheet_name=sheets,startrow=row , startcol=0)   
        row = row + len(dataframe.index) + spaces + 1
    writer.save()

# list of dataframes
dfs = [final_fact,total_dataframe,product_wise_total]

# run function
multiple_dfs(dfs, 'New', 'test1.xlsx', 1)

相关问题