CodeGym /Java Course /Python SELF EN /Writing Data to an Excel File Using the pandas Library

Writing Data to an Excel File Using the pandas Library

Python SELF EN
Level 27 , Lesson 3
Available

Hey there, coder friends! Hope everything's going great with you, and if not, I hope this lecture makes your day a little better. Today, we're gonna learn how to write data into an Excel file using the lovely and friendly pandas library. Yep, the same one we've all heard about. This lecture is an important step towards automating everything, like writing reports or even tracking personal finances. If you've ever dreamt of becoming the guru of office table automation (and who hasn't?), then you're on the right track.

1. Basics of Writing Data to Excel with pandas

Alright, let's say we have a DataFrame filled with numbers and text, and we want to turn this masterpiece into an Excel file. How do we do it with pandas? Super simple! The pandas library has this awesome method called to_excel, which solves all our data-saving problems.

How does it work?

To write a DataFrame to an Excel file, we use the to_excel method. This function allows us to specify the name of the file we want to save to and even, for example, set the name of the sheet our data will go on. Let's take a quick example:

Python

import pandas as pd

# Let's create a small DataFrame
data = {
    'Fruits': ['Apple', 'Banana', 'Cherry'],
    'Quantity': [10, 5, 2]
}
df = pd.DataFrame(data)

# Write the DataFrame to an Excel file
df.to_excel('fruits.xlsx', sheet_name='Favorite Fruits', index=False)

print("Data successfully written to 'fruits.xlsx'")
    

A bit more about the parameters

  • File name: We specified 'fruits.xlsx'. Don't forget to add the '.xlsx' extension, or you'll end up with a file Excel won't recognize.
  • sheet_name: Here, we set it to 'Favorite Fruits'. This is the name of the sheet that will hold the data.
  • index: By default, pandas writes the DataFrame's index as the first column. By setting index=False, we avoid this.

2. Working with Multiple Sheets

How about writing data to multiple sheets in one Excel file? The more data, the more fun, right? Below is an example of how to do it.

We're gonna use pandas.ExcelWriter, which gives us more flexibility when working with files.

Python

# Creating two DataFrames
data1 = {'Fruits': ['Apple', 'Orange'], 'Quantity': [5, 7]}
data2 = {'Vegetables': ['Carrot', 'Cabbage'], 'Quantity': [3, 4]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Creating an Excel file with multiple sheets
with pd.ExcelWriter('produce.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Fruits', index=False)
    df2.to_excel(writer, sheet_name='Vegetables', index=False)

print("Data successfully written to 'produce.xlsx' across different sheets.")
    

Why would you need this?

Working with multiple sheets is useful when data is logically divided into categories, like reports for different months, different product types, etc. Excel becomes a universal container, allowing you to keep all your data in one file, neatly distributed across sheets.

3. Practical Task

Now it's time for a little challenge. You'll create a new Excel file containing data about book sales in a store. You'll have two sheets: one for January book sales and another for February. Here's a bit of data to help you get started:

  • January: book "Python for Everyone" (sales: 50), "JavaScript Secrets" (sales: 30).
  • February: book "Python for Everyone" (sales: 70), "JavaScript Secrets" (sales: 40).

Try implementing this task yourself, then check out the example below if you need a hand.

Python

# Example solution

# Data for January
january_data = {'Book': ['Python for Everyone', 'JavaScript Secrets'], 'Sales': [50, 30]}
january_df = pd.DataFrame(january_data)

# Data for February
february_data = {'Book': ['Python for Everyone', 'JavaScript Secrets'], 'Sales': [70, 40]}
february_df = pd.DataFrame(february_data)

# Creating an Excel file with data for two months
with pd.ExcelWriter('book_sales.xlsx') as writer:
    january_df.to_excel(writer, sheet_name='January', index=False)
    february_df.to_excel(writer, sheet_name='February', index=False)

print("Data successfully written to 'book_sales.xlsx'.")
    

Check out the book_sales.xlsx file, open it, and admire the sheets you created!

4. Discussing Solutions and Optimization

As you may have noticed, writing data to Excel using pandas is pretty straightforward. However, here are a few things to keep in mind:

  • Memory usage optimization: If you have huge DataFrames, consider splitting the data into chunks and writing them sequentially.
  • Formatting: You can add formatting, like styles for table headers. This requires using an additional library like openpyxl and its methods.
  • Multiple writes: If you plan to write to the same file often, consider using mode='a' in ExcelWriter to append new data to the existing file.

A few examples to improve your future work

In the future, when you become the king (or queen?) of automation, you'll need to find and fix problems. For example, if your company decides to compile daily reports for each month, you can use pandas and ExcelWriter to automate this process. Maybe even automate checking for weekends and holidays to skip accounting for those days.

So don't be afraid to experiment and optimize your work! Python and pandas are just the tools to help you out.

1
Task
Python SELF EN, level 27, lesson 3
Locked
Write the main DataFrame to Excel
Write the main DataFrame to Excel
2
Task
Python SELF EN, level 27, lesson 3
Locked
Writing multiple DataFrames to a single Excel file
Writing multiple DataFrames to a single Excel file
3
Task
Python SELF EN, level 27, lesson 3
Locked
Working with a complex Excel file with multiple sheets
Working with a complex Excel file with multiple sheets
4
Task
Python SELF EN, level 27, lesson 3
Locked
Automation of data recording and report generation
Automation of data recording and report generation
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION