Skip to content

Commit

Permalink
fix merge methods
Browse files Browse the repository at this point in the history
  • Loading branch information
itlubber committed Jan 22, 2024
1 parent e29da22 commit 929b0bc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions scorecardpipeline/excel_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,26 +271,37 @@ def insert_df2sheet(self, worksheet, data, insert_space, merge_column=None, head
df = df.sort_values(merge_column).reset_index(drop=True)

merge_cols = [get_column_letter(df.columns.get_loc(col) + column_index_from_string(start_col)) for col in merge_column]
merge_rows = list(np.cumsum(df.groupby(merge_column)[merge_column].count().values[:, 0]) + start_row + 1)
if header:
merge_rows = list(np.cumsum(df.groupby(merge_column)[merge_column].count().values[:, 0]) + start_row + df.columns.nlevels)
else:
merge_rows = list(np.cumsum(df.groupby(merge_column)[merge_column].count().values[:, 0]) + start_row)
else:
merge_cols = None
merge_rows = None

def _iterrows(df, header=True, index=True):
def _iter_rows(df, header=True, index=True):
for i, row in enumerate(dataframe_to_rows(df, header=header, index=index)):
columns = df.columns.tolist()
if header:
if index:
if i == df.columns.nlevels:
continue
elif i == df.columns.nlevels - 1:
yield list(df.index.names) + row[df.index.nlevels:]
yield list(df.index.names) + [c[i] for c in columns]
continue
elif i < df.columns.nlevels - 1:
yield [None] * df.index.nlevels + [c[i] for c in columns]
continue
else:
if i < df.columns.nlevels:
yield [c[i] for c in columns]
continue
else:
if index and i == 0:
continue
yield row

for i, row in enumerate(_iterrows(df, header=header, index=index)):
for i, row in enumerate(_iter_rows(df, header=header, index=index)):
if fill:
if header and i < df.columns.nlevels:
self.insert_rows(worksheet, row, start_row + i, start_col, style="header", auto_width=auto_width, multi_levels=True if df.columns.nlevels > 1 else False)
Expand Down Expand Up @@ -691,4 +702,5 @@ def dataframe2excel(data, excel_writer, sheet_name=None, title=None, header=True
end_row, end_col = dataframe2excel(multi_sample, writer, sheet_name="模型报告", start_row=end_row + 2, title="测试样例", index=True, header=False)
end_row, end_col = dataframe2excel(multi_sample, writer, sheet_name="模型报告", start_row=end_row + 2, title="测试样例", index=True)
end_row, end_col = dataframe2excel(multi_sample, writer, sheet_name="模型报告", start_row=end_row + 2, title="测试样例", index=True, fill=False)
end_row, end_col = dataframe2excel(multi_sample.reset_index(names=multi_sample.index.names, col_level=-1), writer, sheet_name="模型报告", start_row=end_row + 2, title="测试样例", index=False, fill=False, merge_column=[('', '考试类型'), ('', '姓名')])
writer.save("测试样例.xlsx")

0 comments on commit 929b0bc

Please sign in to comment.