问题:一个df进行分组一个中等大小的df1, 之后再对df1进行二次分组, 二次分组的每个df保存到ws工作簿的不同sheet中,然后对每个sheet的数字格式进行处理,现在问题是:只有第一个sheet的数字变成百分制了,第二个sheet到组后的sheet都是没有完成数字变成百分制了,请帮我看下问题出在哪里,如何修改。
补充:以下为代码
df=pd.read_excel(path)
FH = 0
grouped=df.groupby('Cam') # 根据列'Cam'的值进行分组
for name, group in grouped: # 对分组内容进行解压,得到组名和小组数据
# 保存原始表和结果表
path_save = r"C:\Users\dayin\Desktop\K\KSPLIT.xlsx"
new_path = path_save.split('.')[0] + '_' + name + '.' + path.split('.')[1] # 路径工作簿名称随着这name名称而变化
wb = openpyxl.Workbook() # 创建一个工作簿
grouped_result = group.groupby('Entity')
for name_1, group_1 in grouped_result:
group_1=group_1.reset_index(drop=True) #重置索引不改变原有数据列名
ws= wb.create_sheet(name_1)
for r in dataframe_to_rows(group_1, index=False, header=True):
ws.append(r)
# sheet1原始数据应用格式
# 定义样式
percent_style = NamedStyle(name="percent_style", number_format="0.00%") # 百分比格式处理
float_style = NamedStyle(name="decimal_style", number_format="0.00") # 2位小数格式处理
for row in ws.iter_rows(min_row=2, min_col=2): # 数据区域,从第二行开始格式处理
for cell in row:
if cell.column in [18, 23, 24]: # 限定百分比格式的列,数字1表示第1列
cell.style = percent_style
# elif cell.column in [100]: # 限定2位小数的列,数字5表示第5列
# cell.style = float_style
# 保存文件
sheet_names = wb.sheetnames
del wb[sheet_names[0]]
wb.save(new_path) # 保存动作