This tutorial explains about How to read and write CSV using the Pandas library in Python.

Pandas library is used to process data and also read and write files.

CSV file contains multiple lines, each line contains comma separated values.

CSV file contains an optional header.

Let’s declare CSV file

id,name,salary,department
1,john,2000,sales
2,Andrew,5000,finance
3,Mark,8000,hr
4,Rey,5000,marketing
5,Tan,4000,IT

In the above example, First row is a header of columns(id, name, salary, department), second row onards contains data of rows. CSV data is of rows and columns which is 2 dimensional data structure. Python pandas provides DataFrame class to store these data structure

Read CSV data using panda library

This example read the CSV file and store into DataFrame object. Let’s see multiple examples

Read CSV file into Pandas Dataframe without Header:

  • First import pandas into a code
  • use read_csv function to read csv file
    • Another optional parameter header=None to ignore column names, can take other parameter delimiter skiprows and index columns. You can check more options here
import pandas as pd

# Reading a CSV file into a DataFrame
dataframe = pd.read_csv('employee.csv',header=None)

# Displaying the DataFrame
print(df)

Parse CSV file into Pandas Dataframe with Header:

In this columns are provided, It reads the columns from the CSV file, and data is stored in the Data frame.

import pandas as pd

# Reading a CSV file without a header and providing column names
columns = ['id', 'name', 'salary','department']
csvdata = pd.read_csv('example_no_header.csv', header=None, names=columns)
# Displaying the DataFrame
print(csvdata)

Write Dataframe data to CSV file

This example initializes the data into a dataframe and saves the data to CSV FIle in Pandas.

  • Create a object , initialized with data
  • Create a DataFrame object using pd.DataFrame(obj)
  • use to_csv() method to create CSV file from dataframe, index=False indicates data prevents writing indices to CSV file

Here is an example for saving to CSV file

import pandas as pd

# CSV Example data
obj = {
    'id': [1, 2, 3,4,5],
    'name': ['john', 'Andrew', 'Mark','Rey','Tan'],
    'Age': [2000, 5000, 8000, 5000, 4000],
    'department': ['sales', 'finance', 'hr','marketing','IT']
}

# Create a DataFrame
df = pd.DataFrame(obj)

# Specify the file path for the CSV file
csv_file = 'output.csv'

# Save the DataFrame to a CSV file
df.to_csv(csv_file, index=False)

print(f'DataFrame saved to {csv_file}')

After Running the above code, It creates a CSV file.