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.
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.
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.
# 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
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)
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)
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')
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 !!