Python - Read csv file with Pandas without header?

PythonServer Side ProgrammingProgramming

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.

Let’s say the following are the contents of our CSV file opened in Microsoft Excel −

At first, import the required library −

import pandas as pd

Load data from a CSV file into a Pandas DataFrame. This will display the headers as well −

dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")

While loading, use the header parameter and set None to load the CSV without header −

pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv", header=None)

Example

Following is the code −

import pandas as pd

# Load data from a CSV file into a Pandas DataFrame
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
print("\nReading the CSV file...\n",dataFrame)

# Load data from a CSV file and hide the header
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv", header=None)
print("\nReading the CSV file (without header)...\n",dataFrame)

Output

This will produce the following output −

Reading the CSV file...
       Car   Reg_Price   Units
0      BMW        2500     100
1    Lexus        3500      80
2     Audi        2500     120
3   Jaguar        2000      70
4  Mustang        2500     110

Reading the CSV file (without header)...
0 1 2
0      Car   Reg_Price Units
1      BMW        2500 100
2    Lexus        3500 80
3     Audi        2500 120
4   Jaguar        2000 70
5  Mustang        2500 110
raja
Updated on 30-Sep-2021 11:46:49

Advertisements