1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import pandas as pd import datetime
file_path = input("请输入文件地址:")
df = pd.read_excel(file_path.strip("'"), skiprows= 1, dtype={'学号': str, '卡号': str})
print(df)
print('分类前行数', len(df)) print('分类前金额总和', df['金额'].sum()/2) print()
df['卡号'] = df['卡号'].fillna('').astype(str).str.strip().str.replace(' ', '')
df['学号'] = df['学号'].astype(str).apply(lambda x: x.zfill(7))
filtered = df.groupby(['学号', '姓名'], as_index=False).filter(lambda x: len(x) != 1).sort_values(['学号', '姓名'])
print('做了多份工作的同学:') print(filtered[['学号', '姓名', '金额']]) print()
df = df.groupby(['学号', '姓名', '卡号'], as_index=False)['金额'].sum()
print('分类后行数', len(df)) print('总金额', df['金额'].sum())
for i in range(5): df.insert(2, f'{i+1}', '')
df = df.reindex(columns=['姓名', '卡号', 1, 2, 3, 4, 5, '金额', '学号'])
output_file = file_path.strip("'").replace(".xlsx", '_' + datetime.datetime.now().strftime('%m月%d日%H%M') + '_加密表.xlsx')
with pd.ExcelWriter(output_file) as writer: df.to_excel(writer, sheet_name='Sheet1', index=False)
print('输出结果已保存到文件中!')
|