Table of Contents
Pandas is a popular open-source library in Python used for data analysis and manipulation. One of its functionalities is to export data from a DataFrame into various file formats, such as CSV. To export specific columns from a Pandas DataFrame to a CSV file, one can use the “to_csv()” method and specify the desired columns using the “columns” parameter. This allows for a more efficient and organized way of exporting data, particularly when dealing with large datasets. By utilizing this feature, users can easily extract and export only the necessary information from a DataFrame, saving time and effort in the data analysis process.
Pandas: Export Specific Columns in DataFrame to CSV File
You can use the following syntax to export only specific columns from a pandas DataFrame to a CSV file:
df.to_csv('my_data.csv', columns=['col1', 'col4', 'col6'])
The columns argument tells pandas which specific columns to export from the DataFrame to the CSV file.
The following example shows how to use this syntax in practice.
Example: Export Specific Columns from Pandas DataFrame to CSV File
Suppose we have the following pandas DataFrame that contains information about various basketball players:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': [18, 22, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print(df) team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7 H 28 4 12
If we use the to_csv() function to export the DataFrame to a CSV file, pandas will export all of the columns by default:
#export DataFrame to CSV file
df.to_csv('basketball_data.csv')
Here is what the CSV file looks like:

Notice that all of the columns from the DataFrame are included in the CSV file.
To export only specific columns from the DataFrame to a CSV file, we can use the columns argument.
For example, we can use the following syntax to export only the team and rebounds columns to the CSV file:
#export only team and rebounds columns from DataFrame to CSV file
df.to_csv('basketball_data.csv', columns=['team', 'rebounds'])Here is what the CSV file looks like:

Note: You can find the complete documentation for the pandas to_csv() function .
The following tutorials explain how to perform other common tasks in pandas:
How to Export Data to CSV File with No Header in Pandas
How to Merge Multiple CSV Files in Pandas
Cite this article
stats writer (2024). How can I export specific columns from a Pandas DataFrame to a CSV file?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-export-specific-columns-from-a-pandas-dataframe-to-a-csv-file/
stats writer. "How can I export specific columns from a Pandas DataFrame to a CSV file?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-export-specific-columns-from-a-pandas-dataframe-to-a-csv-file/.
stats writer. "How can I export specific columns from a Pandas DataFrame to a CSV file?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-export-specific-columns-from-a-pandas-dataframe-to-a-csv-file/.
stats writer (2024) 'How can I export specific columns from a Pandas DataFrame to a CSV file?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-export-specific-columns-from-a-pandas-dataframe-to-a-csv-file/.
[1] stats writer, "How can I export specific columns from a Pandas DataFrame to a CSV file?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I export specific columns from a Pandas DataFrame to a CSV file?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
