A “Text” file used to store the data in a table structured format is referred to as a “csv” file. The Pandas DataFrame represents data in well-organized 2-dimensional rows and columns format. To export data from Pandas DataFrame into a “csv” file, the “df.to_csv()” function is used in Python.
This post will address the suitable method to export Pandas DataFrame to a CSV File.
Using df.to_csv() Function to Export Pandas DataFrame to a CSV File
The “df.to_csv()” function is used to export a Pandas DataFrame into CSV file data. Let’s comprehend this method using the below-given examples in Python.
Example 1: Exporting the Pandas DataFrame to a CSV File
The below code snippet demonstrates how to export a data frame to a CSV:
Code:
import pandas
data_frame = pandas.DataFrame({'Name': ['Alex','Joseph','Lily'],
'Age': [22,32,12],
'Height': [5.7,5.2,3.4]})
data_frame.to_csv(r'ILF.csv', index=False)
In the above code:
- The “pd.DataFrame()” function is to create a DataFrame and the “df.to_csv()” function to export DataFrame into CSV.
- The “df.to_csv()” returned the DataFrame into a CSV file named “ILF.csv”.
- The parameter “Index=False” indicates that the default index of the csv file can be used rather than the DataFrame index.
Output:
The above output shows that the DataFrame content has been successfully written into a CSV file.
Example 2: Exporting Specific Columns of Pandas DataFrame to CSV File
The specific columns of DataFrame can be exported into csv file in the following example code:
Code:
import pandas
data_frame = pandas.DataFrame({'Name': ['Alex','Joseph','Lily'],
'Age': [22,32,12],
'Height': [5.7,5.2,3.4]})
data_frame[['Name', 'Height']].to_csv(r'ILF.csv', index=False)
In the above code:
- The “pandas.DataFrame()” function with multiple columns is initialized in the program.
- The “Name” and “Height’ column names of the data frame are passed inside the DataFrame function such as “data_frame([[‘Name’, ‘Height’]])” to convert into a csv file.
Output:
The output demonstrates that only the specified columns are exported to the CSV file.
Example 3: Exporting the Pandas DataFrame to CSV File Using User-Defined Index
In the below example, the DataFrame data is exported into csv file along with the user-defined index:
Code:
import pandas
data_frame = pandas.DataFrame({'Name': ['Alex','Joseph','Lily'],
'Age': [22,32,12],
'Height': [5.7,5.2,3.4]})
data_frame.to_csv(r'ILF.csv', index=True)
In the above code:
- The parameter “index=True” is passed inside the “df.to_csv()” function to create the user-defined index while exporting data into a csv file.
Output:
The above output shows that the DataFrame has been exported to a CSV file with the user-defined indexes.
Conclusion
To export pandas DataFrame to a comma-separated file, the “df.to_csv()” function is utilized in the Python program. We can also export specific columns of DataFrame into a csv file using this function. The “df.to_csv()” function is also exported into pandas DataFrame with a user-defined index. This guide provided an in-depth tutorial on exporting pandas DataFrame to CSV file.