Automate Multi‑Sheet Excel Scoring with Python & Pandas: Step‑by‑Step Guide
This article walks through using Python and pandas to batch‑process seven Excel evaluation sheets, skipping header rows, cleaning data, computing total and average scores per person, merging results, and outputting aggregated statistics, providing a practical automation solution for repetitive office tasks.
Hello, I'm Pi Pi. Recently a member asked how to batch calculate average total scores from seven evaluation Excel sheets using Python.
1. Introduction
The task involves reading multiple Excel files, skipping header rows, cleaning nulls, computing total and average scores per person, and aggregating the results.
2. Implementation
The following Python script uses pandas and pathlib to read all .xls files in a folder, drop empty rows, add total and average columns, concatenate the dataframes, and group by name to obtain summed totals and mean averages.
import pandas as pd
import pathlib
# 获取文件夹中每个Excel文件的路径
folder = r"C:\Users\Desktop\民主评议表"
excel_files = pathlib.Path(folder).glob('*.xls')
header = ['姓名','以学铸魂','以学增智','以学正风','以学促干']
data = []
for i in excel_files:
# 读取Excel文件,并跳过前4行,使用前5列数据
df = pd.read_excel(i, skiprows=4, header=None, index_col=0, usecols="A:F")
df.dropna(inplace=True)
df.columns = header
df['总分'] = df[header[1:]].sum(axis=1)
df['平均分'] = df[header[1:]].mean(axis=1)*4
data.append(df)
# 纵向拼接data中的df数据
df = pd.concat(data, axis=0).reset_index(drop=True)
# 按照'姓名'列进行分组,并计算每个姓名的总分和平均分
result = df.groupby('姓名', as_index=False).agg(总分=('总分','sum'), 平均分=('总分','mean')).sort_values(by='平均分', ascending=False)
print(result)
# 将结果保存到新的Excel文件中
# result.to_excel('output.xlsx', index=True)Running the script produces the expected aggregated scores, matching manual calculations.
The solution demonstrates how Python can automate repetitive Excel statistics tasks efficiently.
3. Conclusion
This example shows a practical Python automation workflow for batch processing Excel files, calculating totals and averages per individual, and saving the results, helping users avoid tedious manual work.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
