|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Python+Pandas:
import pandas as pd
import numpy as np
f = pd.ExcelFile('pandas exercise/sample546.xlsx')
df = f.parse('分班及座位表')
def f(total_nos, rooms):
nos_per_room = total_nos // rooms
standard_room = [i for i in range(1, nos_per_room + 1)]
if total_nos % rooms == 0:
dct = {i: standard_room for i in range(1, rooms + 1)}
else:
remains = total_nos % rooms
bigger_room = standard_room + [standard_room[-1] + 1]
dct = {i: bigger_room for i in range(1, remains + 1)}
dct.update({i: standard_room + [np.nan] for i in range(remains + 1, rooms + 1)})
df = pd.DataFrame(dct).stack().reset_index().drop('level_0', axis=1)
df.columns = ['考场号', '座位号']
df.index = pd.Series(np.random.choice(range(total_nos), total_nos, replace=False))
return df
df1 = f(len(df), 17)
df = df.iloc[:, [0,1,2,5,6]].join(df1)
wb = pd.ExcelWriter('pandas exercise/Sample546_results.xlsx')
for name, group in df.groupby('考场号', as_index=False):
group.to_excel(wb, str(name), index=False)
wb.close()
|
|