How to read CSV without headers in pandas

Spread the love

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.


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.


   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).


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

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)

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

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