Writing Data
pandas
can also write data to a variety of file formats, including CSV, Excel, and SQL databases. The following code cell writes the elections
dataset to a CSV file named elections.csv
.
To write a DataFrame to a CSV file, use the df.to_csv()
function. The first input to df.to_csv()
is the filename or filepath that you want to write to.
'elections_new.csv') pd.to_csv(
Other important parameters of the df.to_csv()
function are:
sep
: (default:sep=','
) specifies the separator used to separate columns. Default is,
which means the columns are separated by a comma.header
: (default:header=True
) specifies whether to write the header row. Default isTrue
which means the header row is written. If you don’t want to write the header row, thenheader=False
should be used.index
: (default:index=True
) specifies whether to write the index column. Default isTrue
which means the index column is written. If you don’t want to write the index column, thenindex=False
should be used. data.to_csv(‘elections.csv’)