How to read CSV without headers in pandas

In order to read a CSV file without headers use None value to header param in pandas read_csv() function. In this article, I will explain different header param values {int, list of int, None, default ‘infer’} that support how to load CSV with headers and with no headers.

1. Read CSV without Headers

By default, pandas consider CSV files with headers (it uses the first line of a CSV file as a header record), in case you wanted to read a CSV file without headers use header=None param.

read csv without header
CSV without header

When header=None used, it considers the first record as a data record.


# Read csv without header
df = pd.read_csv('/Users/admin/apps/courses.csv', header=None)
print(df)

Yields below output.

0 1 2 3 0 Spark 25000 50 Days 2000 1 Pandas 20000 35 Days 1000 2 Java 15000 NaN 800 3 Python 15000 30 Days 500 4 PHP 18000 30 Days 800

2. Set Header Names

When the header is ignored it assigned numerical numbers as column names. To fix this read_csv() function provides a param names to assign custom column names while reading.


# Set Header Names
columns = ['courses','course_fee','course_duration','course_discount']
df = pd.read_csv('/Users/admin/apps/courses.csv', header=None, names=columns)
print(df)

Yields below output.


# Output:
   courses course_fee course_duration course_discount
0    Spark      25000         50 Days            2000
1   Pandas      20000         35 Days            1000
2     Java      15000             NaN             800
3   Python      15000         30 Days             500
4      PHP      18000         30 Days             800

3. pandas Read CSV with Header

As I said above, by default it reads the CSV with header (Considers the first row as header). If you have wanted to consider at Nth row use header=N param (replace N according to you need).


# Pandas Read CSV with Header
df = pd.read_csv('/Users/admin/apps/courses.csv')
print(df)

4. Ignore Header Record

Assumes that you have a header with column names as the first row on CSV file and you wanted to ignore this while reading, to do so use skiprows=1 and assign new column names as explained above.

read CSV without Header

# Ignore Header Record
columns = ['courses','course_fee','course_duration','course_discount']
df = pd.read_csv('/Users/admin/apps/courses.csv', header=None, 
     names=columns, skiprows=1)
print(df)

By not using skiprows=1 param, it converts the header row with column names as data records.

Conclusion

In this article, you have learned how to read a pandas CSV file with and without headers, If you don’t have a header use header=None. If you have a header then use skiprows=1 and use header=None.

Happy Learning !!

Related Articles

References

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ @ LinkedIn

Leave a Reply

You are currently viewing How to read CSV without headers in pandas