Writing a DataFrame to a file

Report a typo

Suppose you have a DataFrame that looks like this:

+--------+---------------+-------------------+
|        | Title         | Author            |
|--------+---------------+-------------------|
|   Year |               |                   |
|--------+---------------+-------------------|
|   1877 | Anna Karenina | Leo Tolstoy       |
|   1815 | Emma          | Jane Austen       |
|   1860 | Little Women  | Louisa May Alcott |
|   1847 | Jane Eyre     | Charlotte Brontë  |
+--------+---------------+-------------------+

Note that Year is an index, not an ordinary column. Which of the following options will save this DataFrame to a books.csv file that looks like this:

Year,Title
1877,Anna Karenina
1815,Emma
1868,Little Women
1847,Jane Eyre

Select the correct answer.

You can try various options with the following starter code:

import pandas as pd

books = pd.DataFrame({'Year': [1877, 1815, 1860, 1847],
                   'Title': ['Anna Karenina', 'Emma', 'Little Women', 'Jane Eyre'],
                   'Author': ['Leo Tolstoy', 'Jane Austen', 'Louisa May Alcott', 'Charlotte Brontë']})

books.set_index('Year', inplace=True)
Select one option from the list
___

Create a free account to access the full topic