Get startedGet started for free

Customizing a CSV file

Sometimes, data needs to be stored in a CSV file in a customized manner. This may include using different header values, including or excluding the index column of a DataFrame, or altering the character used to separate columns. In this example, you'll get to practice this, as well as ensuring that the file is stored in the desired file path.

The pandas library has been imported as pd, and the data has already been transformed to include only rows with a "Quantity Ordered" greater than one. The cleaned DataFrame is stored in a variable named clean_sales_data.

This exercise is part of the course

ETL and ELT in Python

View Course

Exercise instructions

  • Import the os library.
  • Write the cleaned DataFrame to a CSV stored at path_to_write, without a header.
  • Ensure the file was written to the desired path.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import the os library
____

# Load the data to a csv file with the index, no header and pipe separated
def load(clean_data, path_to_write):
	clean_data.____(____, header=____, sep="|")

load(clean_sales_data, "clean_sales_data.csv")

# Check that the file is present.
file_exists = os.____.____(____)
print(file_exists)
Edit and Run Code