To save Panda's DataFrame in to CSV or Excel file, use following commands...
- df.to_csv('data.csv', index=False)
- df.to_excel('data.xls', index=False)
In this notebook, we will learn about saving Pandas Dataframe in to a CSV file.
For this excercise we will use dummy data.
import pandas as pd
Let us first create a Python list of dictionaries where each dictionary contains information about a trading stock.
data = [{'tickr':'intc', 'price':45, 'no_of_employees':100000}, {'tickr':'amd', 'price':85, 'no_of_employees':20000}]
Let us first convert above list to Pandas DataFrame using pd.DataFrame method.
df = pd.DataFrame(data)
df is Pandas Dataframe. Let us print it.
To learn more about Pandas and Dataframes, checkout following notebooks...
https://www.nbshare.io/notebooks/pandas/
print(df)
we can save this data frame using df.to_csv method as shown below. Note the first argument in below command is the file name and second argument 'index=False' will restrict Pandas from inserting row (or index) numbers for each row.
df.to_csv('data.csv', index=False)
Above command shoulde create a 'data.csv' file in our current directory. Let us check that using 'ls' command.
ls -lrt data.csv
yes indeed the file is there. Let us check the contents of this file using Unix 'cat' command.
Note i am running this notebook on Linux machine that is why i am able to run these unix Commands from the Jupyter notebook.
cat data.csv
As we see above, the content is comma separated list of values. Instead of comma, we can use any other separator using the "sep" argument.
df.to_csv('data.csv', index=False,sep="|")
cat data.csv
Note: There are lot of options which df.to_csv can take. Checkout the complete list below...
df.to_csv(
path_or_buf: 'FilePathOrBuffer[AnyStr] | None' = None,
sep: 'str' = ',',
na_rep: 'str' = '',
float_format: 'str | None' = None,
columns: 'Sequence[Hashable] | None' = None,
header: 'bool_t | list[str]' = True,
index: 'bool_t' = True,
index_label: 'IndexLabel | None' = None,
mode: 'str' = 'w',
encoding: 'str | None' = None,
compression: 'CompressionOptions' = 'infer',
quoting: 'int | None' = None,
quotechar: 'str' = '"',
line_terminator: 'str | None' = None,
chunksize: 'int | None' = None,
date_format: 'str | None' = None,
doublequote: 'bool_t' = True,
escapechar: 'str | None' = None,
decimal: 'str' = '.',
errors: 'str' = 'strict',
storage_options: 'StorageOptions' = None,
) -> 'str | None'
Related Notebooks
- How to Export Pandas DataFrame to a CSV File
- How To Write DataFrame To CSV In R
- How to Analyze the CSV data in Pandas
- How To Read CSV File Using Python PySpark
- Convert Pandas DataFrame To Numpy Arrays
- How to Sort Pandas DataFrame with Examples
- 3 Ways to Rename Columns in Pandas DataFrame
- How To Convert Python List To Pandas DataFrame
- How to Convert Python Pandas DataFrame into a List