0
0
Pandasdata~15 mins

Writing to Excel with to_excel in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Writing to Excel with to_excel
📖 Scenario: You work in a small business that tracks monthly sales data. You want to save this data into an Excel file so you can share it with your team easily.
🎯 Goal: Build a simple program that creates a sales data table and writes it to an Excel file named sales_data.xlsx.
📋 What You'll Learn
Create a pandas DataFrame with specific sales data
Set a variable for the Excel file name
Use the to_excel method to write the DataFrame to the Excel file
Print a confirmation message with the file name
💡 Why This Matters
🌍 Real World
Saving data to Excel files is common in business for sharing reports and summaries with colleagues who use spreadsheet software.
💼 Career
Data analysts and scientists often export data to Excel to communicate results to non-technical team members or managers.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_df with these exact columns and values:
'Month': ['January', 'February', 'March'],
'Sales': [2500, 3000, 2800]
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Set the Excel file name
Create a variable called excel_file and set it to the string 'sales_data.xlsx'.
Pandas
Need a hint?

Just assign the string 'sales_data.xlsx' to the variable excel_file.

3
Write the DataFrame to the Excel file
Use the to_excel method on sales_df to write the data to the file named in excel_file. Do not write the DataFrame index to the file (use index=False).
Pandas
Need a hint?

Call sales_df.to_excel(excel_file, index=False) to save the file without the index column.

4
Print confirmation message
Print the message "Data saved to sales_data.xlsx" using the excel_file variable inside the message with an f-string.
Pandas
Need a hint?

Use print(f"Data saved to {excel_file}") to show the message.