Advanced Excel Data Processing and Summarization with Python Pandas
This tutorial demonstrates how to use Python's Pandas library to read, clean, filter, sort, deduplicate, group, pivot, and export Excel data, providing step‑by‑step code examples that turn large spreadsheets into concise, analyzable summaries.
In modern workplaces Excel is ubiquitous, but handling thousands of rows manually is inefficient; Python, with its powerful Pandas library, can automate data cleaning, analysis, and summarization.
Pandas offers the DataFrame structure, ideal for Excel‑like tabular data, enabling easy reading, filtering, grouping, aggregation, and exporting back to Excel.
Assume an Excel file sales_data.xlsx containing columns Region, Month, Product, Quantity, and Sales, with sample rows such as East‑Jan‑A‑100‑2000, West‑Feb‑B‑180‑3600, etc.
Code examples:
import pandas as pd
df = pd.read_excel('sales_data.xlsx')
print(df.head()) # Convert Sales column to numeric
df['Sales'] = pd.to_numeric(df['Sales'], errors='coerce')
print(df['Sales'].dtypes) # Filter rows where Sales > 1000
filtered_df = df[df['Sales'] > 1000]
print(filtered_df) # Sort by Sales descending
sorted_df = df.sort_values(by='Sales', ascending=False)
print(sorted_df) # Remove duplicate rows
unique_df = df.drop_duplicates()
print(unique_df) # Fill missing values with 0
filled_df = df.fillna(0)
print(filled_df) # Group by Region and sum Sales
grouped_df = df.groupby('Region')['Sales'].sum().reset_index()
print(grouped_df) # Group by Region and sum both Sales and Quantity
summary_df = df.groupby('Region')[['Sales', 'Quantity']].sum().reset_index()
print(summary_df) # Create a pivot table of Sales by Month and Product
pivot_table = pd.pivot_table(df, values='Sales', index='Month', columns='Product', aggfunc=sum)
print(pivot_table) # Export processed data back to Excel
processed_df = df # Perform any additional processing here
processed_df.to_excel('processed_sales_data.xlsx', index=False)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.
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.
