Recall & Review
beginner
What does the
to_csv function do in pandas?It saves a DataFrame to a CSV file, turning the table of data into a text file with comma-separated values.
Click to reveal answer
beginner
How do you save a DataFrame
df to a file named data.csv?Use
df.to_csv('data.csv'). This writes the DataFrame to a CSV file called data.csv in your current folder.Click to reveal answer
beginner
What does the parameter
index=False do in to_csv?It prevents pandas from writing the row numbers (index) to the CSV file, so only the data columns are saved.
Click to reveal answer
intermediate
How can you save a DataFrame without the header row using
to_csv?Set
header=False in to_csv. For example, df.to_csv('file.csv', header=False) saves the data without column names.Click to reveal answer
intermediate
Can
to_csv write to a file path or a buffer like a string object?Yes. You can give it a file path as a string or a buffer object like
StringIO to capture the CSV data in memory.Click to reveal answer
What is the default separator used by
to_csv?✗ Incorrect
The default separator for
to_csv is a comma, which is why it creates comma-separated values files.How do you prevent pandas from writing row indices to the CSV file?
✗ Incorrect
Setting
index=False tells pandas not to write the row indices to the CSV.Which parameter controls whether column names are saved in the CSV?
✗ Incorrect
The
header parameter controls if column names are written to the CSV file.What happens if you call
df.to_csv('file.csv') without extra parameters?✗ Incorrect
By default,
to_csv saves both the index and header unless told otherwise.Can
to_csv write CSV data to a string in memory?✗ Incorrect
You can use a buffer like
StringIO to capture CSV output as a string instead of writing to a file.Explain how to save a pandas DataFrame to a CSV file without including the row index.
Think about the parameter that controls row numbers in the output.
You got /3 concepts.
Describe how you would save a DataFrame to a CSV file without the column headers.
Consider the parameter that controls column names in the output.
You got /3 concepts.