Open In App

Export Specific Columns in DataFrame to CSV File

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A data frame is a tabular data structure similar to a spreadsheet. It has rows and columns. CSV (Comma-Separated Values) is a plain text file format used for storing tabular data. Each line in a CSV file represents a row of data, and the values within a line are separated by commas. This article explores two methods for exporting selected columns from a DataFrame to a CSV file: using pandas’ to_csv() method and numpy’s savetxt() function.

Export Specific Columns in DataFrame to CSV File

Below are the two export specific columns In DataFrame to CSV File:

  • Using pandas to_csv() method: This method simplifies the process of exporting DataFrame columns to a CSV file, requiring minimal code. Selected columns are exported to a file named ‘output.csv’, with the index excluded.
  • Using numpy savetxt() method: Combining DataFrame column selection with numpy‘s savetxt() function offers efficiency and flexibility in exporting specific columns. Parameters such as delimiter, header, format, and comments allow for customized output.

To learn how to export specific columns in DataFrame to CSV, let’s create a dataframe using the following code:

Python3




import pandas as pd
data = {
    'Name': ['Disha', 'Shravan', 'Jeetu', 'Ram'],
    'Age': [21, 23, 24, 22],
    'Gender': ['Female', 'Male', 'Male', 'Male'],
    'Education': ['Graduation','Masters','Masters','Graduation'],
    'Salary':[22000,35000,35000,22000]
}
df = pd.DataFrame(data)
df


Output:

      Name  Age  Gender   Education  Salary
0    Disha   21  Female  Graduation   22000
1  Shravan   23    Male     Masters   35000
2    Jeetu   24    Male     Masters   35000
3      Ram   22    Male  Graduation   22000

Using pandas to_csv() method

This method exports selected DataFrame columns to a CSV file. It’s simple and efficient, requiring very less code to specify columns and file path.

  • The selected columns will be exported to a CSV file named ‘output.csv’ using the to_csv method.
  • The index=False argument ensures that the index column is not included in the output file.

Python3




df.to_csv('output.csv', columns=['Name','Age','Salary'],index=False)


Let’s read the csv column to check.

Python3




df1= pd.read_csv('output.csv')
df1


Output:

Name    Age    Salary
0    Disha    21    22000
1    Shravan    23    35000
2    Jeetu    24    35000
3    Ram    22    22000

Using numpy savetxt()

This code snippet combines DataFrame column selection with NumPy’s np.savetxt function to efficiently export specific columns from a DataFrame to a CSV file, handling different data types and formatting the output as desired.

  • delimiter: The character to separate values in the CSV file.
  • header: A string containing the header row with column names. It uses string formatting (','.join(desired_columns)) to create a comma-separated list of column names from the desired_columns list.
  • fmt: A format string where '%s' ensures all data is converted to strings, avoiding potential issues with different data types.
  • comments: An empty string ('') to disable comments in the output file.

Python3




import numpy as np
desired_columns = df[['Age','Education','Salary']]
np.savetxt(
    "output.csv",
    desired_columns,
    delimiter=',',
    header=','.join(desired_columns),
    fmt='%s', # to convert all datatypes to strings
    comments='' # this part is necessary to avoid necessary comments like #
)


Let’s read the csv column to check.

Python3




df1=pd.read_csv('output.csv')
df1


Output:

    Age    Education    Salary
0    21    Graduation    22000
1    23    Masters    35000
2    24    Masters    35000
3    22    Graduation    22000


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads