• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:12 mins read
You are currently viewing How to read CSV without headers in pandas

To read a CSV file without headers use the None value to header param in the 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 want 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 assigns 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 a header (Considers the first row as header). If you have wanted to consider at Nth row use header=N param (replace N according to your need).


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

4. Ignore Header Record

Assume that you have a header with column names as the first row on the CSV file and you want 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 the skiprows=1 param, it converts the header row with column names as data records.

Frequently Asked Questions (FAQ) on Reading CSV Without Headers in Pandas

How do I read a CSV file without headers in Pandas?

You can set the header parameter of the pd.read_csv() function as None to tell Pandas that the CSV file doesn’t have column headers. For example, df = pd.read_csv('file.csv', header=None)

What if I want to assign custom column names to the DataFrame when reading without headers?

You can provide a list of column names using the names parameter of the read_csv() function while reading a CSV file without a header. For example, custom_column_names = ['Column1', 'Column2', 'Column3']
df = pd.read_csv('file.csv', header=None, names=custom_column_names)

How can I specify the delimiter of the CSV file when reading without headers?

You can use the delimiter or sep parameter to specify the delimiter. The default is a comma (,), but you can change it to another character. For example, df = pd.read_csv('file.csv', header=None, delimiter='\t')

Are there any other parameters I should be aware of when reading CSV files without headers?

Depending on your data, you might want to explore additional parameters such as skiprows skipping a specific number of rows at the beginning of the file or dtype specifying data types for columns. For example, df = pd.read_csv('file.csv', header=None, skiprows=2, dtype={'Column1': int, 'Column2': float})

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 Nelamali

Naveen Nelamali (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 and Medium