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 50Create 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 columnPrint 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