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.
Key Points –
- Use the
pd.read_csv()
function to read CSV files in Pandas, specifying parameters to handle missing headers. - Set the
header
parameter toNone
when reading a CSV without headers to prevent the first row from being treated as column names. - When
header=None
is specified, Pandas will automatically assign default integer column names (e.g., 0, 1, 2, …). - Use the
names
parameter to manually assign column names while reading the CSV, which replaces the default integer column names. - To skip certain rows at the beginning of a CSV (e.g., metadata or descriptions), use the
skiprows
parameter with the number of rows to skip. - If only some columns lack headers while others do have them, consider using
header
andskiprows
in combination to manage different sections.
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.
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 8002. 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.
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.
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.
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 !!