0
0
PandasHow-ToBeginner · 3 min read

How to Set Header in read_csv in pandas

Use the header parameter in pandas.read_csv() to specify which row to use as the header. Set header=0 to use the first row as header, or header=None if the file has no header row.
📐

Syntax

The header parameter in pandas.read_csv() controls which row is used as the column names (header). It accepts:

  • header=0 (default): Use the first row as header.
  • header=None: No header row; pandas assigns default column names.
  • header=n: Use the nth row (0-indexed) as header.
python
pandas.read_csv(filepath_or_buffer, header=0)
💻

Example

This example shows how to read a CSV file with a header in the second row (index 1) and how to read a CSV without any header row.

python
import pandas as pd
from io import StringIO

# Sample CSV with header in second row
csv_data = '''
ignore1,ignore2,ignore3
Name,Age,City
Alice,30,New York
Bob,25,Los Angeles
'''

# Read CSV using second row as header
df_with_header = pd.read_csv(StringIO(csv_data), header=1)

# Read CSV with no header, assign default column names
df_no_header = pd.read_csv(StringIO(csv_data), header=None)

print('DataFrame with header row at index 1:')
print(df_with_header)
print('\nDataFrame with no header:')
print(df_no_header)
Output
DataFrame with header row at index 1: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles DataFrame with no header: 0 1 2 0 ignore1 ignore2 ignore3 1 Name Age City 2 Alice 30 New York 3 Bob 25 Los Angeles
⚠️

Common Pitfalls

Common mistakes include:

  • Not setting header=None when the CSV has no header, causing the first data row to be treated as column names.
  • Using the wrong row index for header, leading to incorrect column names.
  • Forgetting that header is zero-indexed.
python
import pandas as pd
from io import StringIO

csv_data = '''Name,Age,City
Alice,30,New York
Bob,25,Los Angeles
'''

# Wrong: header=None when file has header
df_wrong = pd.read_csv(StringIO(csv_data), header=None)

# Right: header=0 to use first row as header
df_right = pd.read_csv(StringIO(csv_data), header=0)

print('Wrong header usage:')
print(df_wrong)
print('\nCorrect header usage:')
print(df_right)
Output
Wrong header usage: 0 1 2 0 Name Age City 1 Alice 30 New York 2 Bob 25 Los Angeles Correct header usage: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles
📊

Quick Reference

ParameterDescriptionExample
header=0Use first row as header (default)pd.read_csv('file.csv', header=0)
header=NoneNo header row, assign default column namespd.read_csv('file.csv', header=None)
header=nUse nth row (0-indexed) as headerpd.read_csv('file.csv', header=2)

Key Takeaways

Use the header parameter in read_csv to specify which row is the header.
Set header=None if your CSV file has no header row.
Remember header row index is zero-based (0 means first row).
Incorrect header setting can cause data rows to be treated as column names.
You can use any row as header by setting header to that row's index.