我想用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
值应在相应的BP
和RE
下。
我使用下面的代码来实现这一点。直到小计部分(包括小计行),数据显示在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
有人能提出解决这个问题的方法吗?
2条答案
按热度按时间nvbavucw1#
如果我理解正确的话,你想计算
"product"
列中每个字母的值的总和如果是这样,你可以简单地使用
pandas
(docs)的groupby
函数:那么结果是:
dkqlctbz2#
我认为你可以创建所有必要的DataFrames,然后通过multiple_dfs写入Excel: