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.
Bu egzersiz
ETL and ELT in Python
kursunun bir parçasıdırEgzersiz talimatları
- Import the
oslibrary. - Write the cleaned DataFrame to a CSV stored at
path_to_write, without a header. - Ensure the file was written to the desired path.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# 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)