Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

ETL and ELT in Python

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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)
Code bewerken en uitvoeren