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
| import pandas as pd
file_path = input("请输入文件地址:")
df = pd.read_excel(file_path.strip("'"))
df['银行卡号'] = df['银行卡号'].fillna('').astype(str).str.strip()
print('分类前行数', len(df)) print('分类前金额总和', df['金额'].sum()) print()
df['学号'] = df['学号'].astype(str).apply(lambda x: x.zfill(7))
df = df.groupby(['学号', '姓名', '银行卡号'], as_index=False)['金额'].sum()
print('分类后', len(df)) print('总金额', df['金额'].sum())
for i in range(5): df.insert(2, f'{i+1}', '')
output_file = input("请输入保存文件的地址:")
with pd.ExcelWriter(output_file) as writer: df.to_excel(writer, sheet_name='Sheet1', index=False) writer.save()
print('输出结果已保存到文件中!')
|