添加小计合计的数据行

	//添加合计行
	private void addSumRow(KDTable table){
//		table.checkParsed();
		if(table.getRowCount() == 0 || table.getRowCount() < 0){
			return;
		}
		IRow firstRow = table.getRow(0);
		if(firstRow.getCell("unitName").getValue() == null){
			return;
		}
		Object tempAdmin = firstRow.getCell("unitName").getValue();
		int total = 0;
		int sum = 0;

		int ctRecruitment = 0;
		int sumCtRecruitment  = 0;

		int iRecruitment = 0;
		int sumIRecruitment = 0;

		int desfocRec = 0;
		int sumDesfocRec = 0;

		Object lastAdminOrg = null;
		boolean flag = true;
		for(int i = 0;i<table.getRowCount();i++){
			IRow row = table.getRow(i);
			if(row.getCell("unitName").getValue() == null
					|| !row.getCell("unitName").getValue().equals(tempAdmin)){
				IRow newRow = table.addRow(row.getRowIndex());
				table.setRowCount(table.getRowCount() + 1);
				newRow.getCell("unitName").setValue("小计");
				newRow.getCell("unitName").getStyleAttributes().setHorizontalAlign(HorizontalAlignment.RIGHT);
				newRow.getStyleAttributes().setBackground(Color.lightGray);
				newRow.getStyleAttributes().setLocked(true);
				lastAdminOrg = table.getRow(i - 1).getCell("area").getValue();
				flag = true;
				newRow.getCell("area").setValue(lastAdminOrg);

				tempAdmin = row.getCell("unitName").getValue();

				newRow.getCell("entrys.recNumber").setValue(new Integer(total));
				sum += total;
				total = 0;


			}else{
				lastAdminOrg = row.getCell("area").getValue();
				if(row.getCell("entrys.recNumber").getValue() != null){
					Integer tempTotal = (Integer) row.getCell("entrys.recNumber").getValue();
					total += tempTotal.intValue();
				}
			}
		}
		
		IRow lastRow = table.addRow(table.getRowCount());
		table.setRowCount(table.getRowCount() + 1);
		lastRow.getCell("entrys.recNumber").setValue(new Integer(total));
		lastRow.getCell("area").setValue(lastAdminOrg);
		if(flag){
			lastRow.getCell("area").setValue(table.getRow(table.getRowCount() - 1).getCell("area").getValue());
		}


		lastRow.getStyleAttributes().setBackground(Color.lightGray);
		lastRow.getCell("unitName").setValue("小计");
		lastRow.getCell("unitName").getStyleAttributes().setHorizontalAlign(HorizontalAlignment.RIGHT);
		lastRow.getStyleAttributes().setLocked(true);

		IRow totalRow = table.addRow(table.getRowCount());
		table.setRowCount(table.getRowCount() + 1);
		totalRow.getCell("entrys.recNumber").setValue(new Integer(sum+total));
	
		totalRow.getStyleAttributes().setBackground(Color.cyan);
		totalRow.getCell("unitName").setValue("合计");
		totalRow.getStyleAttributes().setHorizontalAlign(HorizontalAlignment.RIGHT);
		totalRow.getStyleAttributes().setLocked(true);
	}

数据透视表是数据分析工具的一种,它能够通过拖拽字段到、列、值的位置,将大量数据转换成更有意义的汇总视图。Python 中的数据透视功能通常由 pandas 库提供支持,pandas 是一个用于处理表格数据的强大库。 三层添加小计意味着在创建数据透视表时,需要对数据按三个维度进分组,并分别计算每层的合计值或子层级的合计值。下面是如何使用 pandas 的数据透视表功能实现三层添加小计的一个基本步骤: ### 第一步:准备数据 首先,我们需要一些数据集来操作。假设有如下数据集 `df`: ```python import pandas as pd data = { 'Product': ['A', 'B', 'C', 'D', 'E'], 'Sales': [100, 200, 150, 120, 90], 'Region': ['North', 'South', 'East', 'West', 'North'] } df = pd.DataFrame(data) ``` ### 第二步:创建基础透视表 我们可以先创建一个基于两个维度(例如产品和区域)的基本透视表: ```python pivot_table = df.pivot_table(index='Product', columns='Region', values='Sales') print(pivot_table) ``` 这会生成一个类似于如下的透视表: ``` South East West Product A 100 NaN NaN B 200 NaN NaN C 150 NaN NaN D 120 NaN NaN E 90 NaN NaN ``` ### 第三步:添加第三层及小计 为了添加第三层并包含小计,我们可以通过嵌套函数或额外的计算来实现。在这个例子中,我们将假设“产品”类别可以进一步细分(如果数据集允许),并在每个产品的总销售额上添加一个小计。 首先,我们可以计算每个产品在所有地区的总销售额: ```python product_totals = df.groupby('Product')['Sales'].sum().reset_index() print(product_totals) ``` 然后,将这个总销售量加入我们的透视表: ```python # 将总销售额添加到透视表中 total_sales = product_totals.set_index('Product').Sales.tolist() # 添加小计至透视表的最外层 pivot_table['Total'] = total_sales # 确保显示所有单元格包括缺失的地区数据 pivot_table.fillna(0) # 使用0填充缺失的地区销售额 print(pivot_table) ``` 最终得到的透视表可能看起来像这样: ``` South East West Total Product A 100 0 0 100 B 200 0 0 200 C 150 0 0 150 D 120 0 0 120 E 90 0 0 90 Total 660 0 0 660 ``` 以上就是使用 Python 和 pandas 实现三层数据透视表并添加小计的过程。这种方式不仅适用于简单的二维分析,还可以根据需要扩展到更复杂的多维分析场景。记得调整代码以适应您实际的数据结构和需求。如果您有任何其他问题或需要进一步的帮助,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值