IniziaInizia gratis

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.

Questo esercizio fa parte del corso

ETL and ELT in Python

Visualizza il corso

Istruzioni dell'esercizio

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

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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)
Modifica ed esegui il codice