Back to Python
2026-03-086 min read

EXCEL (Python Programming)

Learn EXCEL (Python Programming) step by step with clear examples and exercises.

Title: Python Excel Automation: A full guide to Reading and Writing Excel Files

Why This Matters

Python's pandas library offers an easy and efficient way to read, write, and manipulate data stored in Excel files. By learning how to use this powerful tool, you can automate repetitive tasks, streamline data analysis workflows, and save valuable time. Furthermore, mastering Python Excel automation is a valuable skill for landing interviews and excelling in your career as a data analyst or data scientist.

Prerequisites

Before diving into the core concept of reading and writing Excel files using Python's pandas library, it's essential to have a solid understanding of:

  1. Basic Python programming concepts, such as variables, functions, loops, and conditional statements
  2. Intermediate Python concepts, including lists, dictionaries, and data structures
  3. The pandas library, specifically its DataFrame object and basic operations like indexing, filtering, and sorting
  4. Familiarity with Excel file structure, such as sheets, rows, and columns

Core Concept

Reading Excel Files with Pandas

To read an Excel file using Python's pandas, first install the necessary libraries:

pip install pandas openpyxl xlsxwriter

Now, let's load a sample Excel file named data.xlsx into a DataFrame:

import pandas as pd

Load the Excel file into a DataFrame

df = pd.read_excel('data.xlsx')

print(df)


By default, `pandas` reads the first sheet of the Excel workbook. If your workbook contains multiple sheets, you can specify the sheet name when calling `pd.read_excel()`. For example:

Load a specific sheet by name

df = pd.read_excel('data.xlsx', sheet_name='Sheet2')


### Reading Specific Data from Excel Files

To read specific data from an Excel file, you can use various methods like `iloc`, `loc`, and `at`. For example:

Read a specific cell (A1)

value = df.iloc[0, 0]

print(value)

Read all values in row 2

row_values = df.iloc[1]

print(row_values)

Read all values in column B

column_values = df['B']

print(column_values)


### Writing Excel Files with Pandas

To write the DataFrame `df` to an Excel file, you can use the `to_excel()` function:

Write the DataFrame to an Excel file

df.to_excel('output.xlsx')


By default, `pandas` writes the DataFrame to a new workbook with one sheet named "Sheet1". If you want to write the DataFrame to an existing workbook or create multiple sheets, use the `ExcelWriter` object:

Create a Pandas Excel writer using XlsxWriter as the engine.

writer = pd.ExcelWriter('output.xlsx', engine='xlsxwriter')

Write each DataFrame to a different worksheet.

df.to_excel(writer, sheet_name='Sheet1')

Save the Excel file

writer.save()


### Writing Specific Data to Excel Files

To write specific data to an Excel file, you can use the `iloc`, `loc`, and `at` methods:

Write a value to a specific cell (A1)

df.iloc[0, 0] = 'New Value'

Write all values in row 2 to a specific range of cells (B2:E2)

row_values = df.iloc[1]

for index, value in enumerate(row_values):

df.at['B' + str(index+2)] = value

Worked Example

In this example, we will read data from an Excel file named sales_data.xlsx, calculate the total sales for each region, and write the results back to an Excel file:

  1. Load the Excel file into a DataFrame:
import pandas as pd

df = pd.read_excel('sales_data.xlsx')
print(df)
  1. Calculate the total sales for each region and write the results to an Excel file:

Calculate the total sales for each region

region_totals = df.groupby('Region')['Sales'].sum()

Create a Pandas Excel writer using XlsxWriter as the engine.

writer = pd.ExcelWriter('output.xlsx', engine='xlsxwriter')

Write the region totals to an Excel file

region_totals.to_excel(writer, sheet_name='Region Totals')

Save the Excel file

writer.save()

Common Mistakes

  1. Forgetting to install necessary libraries (pandas, openpyxl, and xlsxwriter)
  2. Not specifying the sheet name when reading or writing an Excel file
  3. Trying to read or write data from cells that don't exist in the Excel file
  4. Writing over the same cell multiple times without clearing its previous content first
  5. Forgetting to save the Excel file after writing data to it
  6. Not handling missing or invalid data properly, which can lead to errors when reading or writing Excel files
  7. Misunderstanding the differences between iloc, loc, and at methods and using them inappropriately

Subheadings under Common Mistakes:

  • Handling Missing Data
  • Handling Invalid Data
  • Using iloc, loc, and at Correctly

Practice Questions

  1. Write a Python script to read an Excel file named employees.xlsx and output the number of employees in each department.
  2. Given an Excel file with sales data, write a script to calculate the average sale per employee and write the result to an Excel file.
  3. Write a Python script to read an Excel file named inventory.xlsx, find the item with the highest stock quantity, and output its name and quantity.
  4. Write a Python script to read an Excel file named expenses.xlsx, calculate the total expenses for each category, and write the results to an Excel file.
  5. Write a Python script to read an Excel file named customers.xlsx, filter customers with a specific region, and output their contact information.

FAQ

Q: How can I read data from a specific cell in an Excel file using Python?

A: You can use the iloc or at method to access specific cells in a DataFrame. For example:

value = df.iloc[0, 0] # Access the value at row 0, column 0

Q: How do I write data to a specific cell in an Excel file using Python?

A: To write data to a specific cell, first ensure that the DataFrame contains the necessary data, then use the iloc, loc, or at method to set the value at the desired location:

df.iloc[0, 0] = 'New Value' # Write 'New Value' to cell A1

Q: How can I write multiple sheets to an Excel file using Python?

A: To write multiple sheets to an Excel file, create a Pandas Excel writer object and call the to_excel() method for each DataFrame, specifying the sheet name:

Create a Pandas Excel writer using XlsxWriter as the engine.

writer = pd.ExcelWriter('output.xlsx', engine='xlsxwriter')

Write each DataFrame to a different worksheet.

df1.to_excel(writer, sheet_name='Sheet1')

df2.to_excel(writer, sheet_name='Sheet2')

Save the Excel file

writer.save()


4. Q: How can I handle missing or invalid data when reading an Excel file using Python?
A: To handle missing or invalid data, you can use various methods like `fillna`, `dropna`, and `isnull`. For example:

Replace missing values with a specified value (e.g., 0)

df = df.fillna(0)

Drop rows containing missing values

df = df.dropna()

Check if a cell contains missing or invalid data

if df.isnull().values.any():

print("Missing or invalid data found.")


5. Q: How can I use iloc, loc, and at correctly in Python?
A: The main difference between `iloc`, `loc`, and `at` is the way they handle indexing. `iloc` uses integer-based indexing, while `loc` uses label-based indexing. `at` allows you to access a specific cell by its coordinates (row and column). For example:

Access the value at row 0, column 0 using iloc

value = df.iloc[0, 0]

Access the value at row 0, column 0 using loc

value = df.loc[0, 'A']

Access the value at cell A1 using at

value = df.at['A', 0]

EXCEL (Python Programming) | Python | XQA Learn