A popular data analysis library named “Pandas” is used with various formats such as CSV, txt, XML, and others. Different operations are performed on the specified data with the help of the Pandas library. Appending data to another file is one such operation. When working with CSV in Python, it’s often useful to append data to the end of a CSV file. To do this, the “df.to_csv()” function of the “pandas” module with the “mode=a” parameter is used in Python.
This write-up will provide you with a complete tutorial on how to append DataFrame of Pandas to CSV using the following content:
- How to Append Pandas DataFrame to CSV in Python?
- Append Pandas DataFrame to CSV in Python
- Append Pandas DataFrame to CSV With Index and Header in Python
How to Append Pandas DataFrame to CSV in Python?
To append Pandas DataFrame to CSV, the “df.to_csv()” function is used in Python. The syntax of the “df.to_csv()” function is shown below:
In the above syntax:
- The “path_or_buf” parameter specifies the path or buffer to the CSV file we want to write to.
- The “sep” parameter indicates the delimiter that is utilized to separate the CSV values. The default delimiter is a comma.
- The “na_rep” specifies the string that is used to represent missing values in the CSV file. The default value is an empty string.
- The “columns” parameter indicates a list of the columns that need to be included in the CSV file. By default, all of the DataFrame columns will be included.
- The “header” parameter indicates a Boolean value specifying whether the column names are written to the CSV file.
- The other parameters also performed specific tasks in a program.
Let’s consider the CSV file named “new.csv” having the following data:
Now, append a Pandas DataFrame to CSV using the below examples:
Example 1: Append Pandas DataFrame to CSV in Python
The following code is used to append Pandas DataFrame to CSV in Python:
data = {'Name': ['Jon', 'Tim', 'Cyndy'],'Age': [25, 26, 15],'Height': [5.0, 5.2, 4.3]}
df = pandas.DataFrame(data)
df.to_csv('new.csv', mode='a', index=False, header=False)
In this code:
- The “pandas” module is imported.
- The dictionary data is passed to the “DataFrame()” method to create the DataFrame with three header rows labels.
- The “df.to_csv()” function accepts the file name, mode, index, and header parameters as an argument to append the Pandas DataFrame to CSV.
Output
The Pandas DataFrame has been appended to CSV.
Example 2: Append Pandas DataFrame to CSV With Index and Header in Python
The below code appends DataFrame to comma-separated value with specified index and header:
data = {'Name': ['Jon', 'Tim', 'Cyndy'],'Age': [25, 26, 15],'Height': [5.0, 5.2, 4.3]}
df = pandas.DataFrame(data)
df.to_csv('new.csv', mode='a', index=True, header=True)
In the above code:
- The “pandas” module is imported, and the dictionary is created.
- The “pandas.DataFrame()” takes the dictionary as an argument and constructs the DataFrame.
- The “index=True” and “header=True” parameter value is passed along with the file name and mode parameter to append the Pandas DataFrame with specified index and header row data.
Output
The append Pandas DataFrame to CSV contains the specified index and header row.
Conclusion
The “df.to_csv()” function of the Pandas module is used to append Pandas DataFrame to CSV or comma-separated value in Python. The specified DataFrame rows append to CSV with a particular index and header using the parameter value. This tutorial has illustrated an in-depth overview of appending Pandas DataFrame to CSV using numbers examples.