0
0
Pandasdata~15 mins

Writing to CSV with to_csv in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Writing to CSV with to_csv
📖 Scenario: You work in a small bakery that keeps track of daily sales data. You want to save this data into a CSV file so it can be shared easily with your team.
🎯 Goal: Create a small sales data table using pandas, set a filename variable, write the data to a CSV file using to_csv, and print a confirmation message.
📋 What You'll Learn
Create a pandas DataFrame called sales_data with columns Item and Quantity and exactly these rows: 'Bread' with 30, 'Cake' with 15, 'Cookie' with 50
Create a variable called filename and set it to the string 'bakery_sales.csv'
Use the to_csv method on sales_data to write the data to the file named in filename without the index column
Print the exact message "Data saved to bakery_sales.csv"
💡 Why This Matters
🌍 Real World
Saving data to CSV files is a common way to share and store tabular data in many jobs, including sales tracking, inventory management, and reporting.
💼 Career
Data analysts and scientists often export their cleaned or processed data to CSV files to share with teammates or use in other software.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with columns Item and Quantity. Add these rows exactly: 'Bread' with 30, 'Cake' with 15, and 'Cookie' with 50.
Pandas
Need a hint?

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

2
Set the filename variable
Create a variable called filename and set it to the string 'bakery_sales.csv'.
Pandas
Need a hint?

Just assign the string 'bakery_sales.csv' to the variable filename.

3
Write the DataFrame to a CSV file
Use the to_csv method on sales_data to write the data to the file named in filename. Make sure to write the file without the index column by setting index=False.
Pandas
Need a hint?

Call sales_data.to_csv with filename and index=False to avoid saving the row numbers.

4
Print confirmation message
Print the exact message "Data saved to bakery_sales.csv" to confirm the file was saved.
Pandas
Need a hint?

Use print with the exact string "Data saved to bakery_sales.csv".