• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Add Header Row to DataFrame

You can add or set a header row to pandas DataFrame when you create a DataFrame or add a header after creating a DataFrame. If you are reading a CSV file without a header, you would need to add it after reading CSV data into Pandas DataFrame.

In this pandas add a header to the DataFrame article, I will explain how to add a header to DataFrame with several examples. The header on pandas DataFrame is used to identify the type of the column and the value it holds.

Key Points –

  • Specify column names during DataFrame creation using the columns parameter to add a header row to the DataFrame.
  • When reading data from a CSV file into a DataFrame, utilize the header parameter to manage the presence or absence of a header row in the file.
  • Use the columns parameter while creating a Pandas DataFrame to explicitly specify the header row.
  • After DataFrame creation, use the rename method to assign names to the columns, effectively adding a header.
  • Convert the first row of the DataFrame into the header by assigning it as the column index using the set_index method.
  • Create a new DataFrame with the header information and concatenate it with the existing DataFrame using concat to add a header row.

1. Quick Examples to Add Header Row to Pandas DataFrame

Below are some quick examples of how to set the header to pandas DataFrame.


# Below are the examples of adding header row to DataFrame

# Example 1: Header values to be added
column_names=["Courses","Fee",'Duration']

# Example 2: Create DataFrame by assigning header
df=pd.DataFrame(technologies, columns=column_names)

# Example 3: Add header row while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)

# Example 4: Add Header to existing DataFrame
df.columns = column_names

2. Add Header Row While Creating a DataFrame

If you are creating a DataFrame manually from the data object then you have an option to add a header row while creating a DataFrame. To create a DataFrame, you would use a DataFrame constructor which takes a columns param to assign the header. It takes a list as a value and the number of values in a list should not exceed the number of columns in DataFrame.


# Import pandas Library
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]

# Header values to be added
column_names=["Courses","Fee",'Duration']

# Create DataFrame by assigning header
df=pd.DataFrame(technologies, columns=column_names)
print(df)

Yields below output.


# Output:
  Courses    Fee Duration
0   Spark  20000   30days
1  Pandas  25000   40days

2. Add Header While Reading CSV into pandas DataFrame

pandas read_csv() method has an option to identify the header that presents in a CSV file, In case if your CSV file doesn’t have a header then you can add a custom header while reading a CSV into Pandas DataFrame.


# Header values to be added
column_names=["Courses","Fee",'Duration']

# Add header row while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)

3. Add Header Row to Existing Pandas DataFrame.

To add a header row to an existing Pandas DataFrame, you can use the columns attribute or the rename method. You have seen how to add in the above sections while creating a DataFrame. Sometimes it’s impossible to know the headers up-front and you may need to add a header to the existing DataFrame.

3.1 Using Columns Attribute

To add a header row to an existing Pandas DataFrame using the columns attribute. For instance, the columns attribute is directly assigned the list of header names. This effectively adds a header row to the existing DataFrame, with each column having the specified name. Adjust the column_names list based on your specific column naming requirements.


import pandas as pd

# Create DataFrame with out header
df=pd.DataFrame([ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ])

# Assign header to Existing DataFrame
column_names=["Courses","Fee",'Duration']
df.columns = column_names
print(df)

3.2 Using Rename Method

Here’s an example of adding a header row to an existing Pandas DataFrame using the rename method.


# Use the rename method to set header names
df = df.rename(columns=dict(zip(df.columns, column_names)))
print(df)

In the above example, the rename method is used with a dictionary that maps the existing column names to the new header names. The zip function is employed to create pairs of old and new column names, and then dict is used to convert these pairs into a dictionary. This dictionary is passed to the columns parameter of the rename method, effectively renaming the columns and adding a header row to the DataFrame. Yields the same output as above.

4. Complete Examples to Add Header Row to Pandas DataFrame


# Import pandas Library
import pandas as pd
technologies = [ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ]

# Header values to be added
column_names=["Courses","Fee",'Duration']

# Create DataFrame by assigning header
df=pd.DataFrame(technologies, columns=column_names)
print(df)

# Add header row while reading a CSV file
df = pd.read_csv('courses.csv', names=column_names)

# Create DataFrame with out header
df=pd.DataFrame([ ["Spark",20000, "30days"], 
                 ["Pandas",25000, "40days"], 
               ])

# Assign header to Existing DataFrame
column_names=["Courses","Fee",'Duration']
df.columns = column_names
print(df)

Frequently Asked Questions on Pandas Add Header Row to DataFrame

How do I add a header row to an existing Pandas DataFrame?

You can add a header row to an existing Pandas DataFrame using either the columns attribute or the rename method. With columns, you directly assign the list of header names to the attribute. With rename, you create a dictionary mapping existing column names to new header names.

Can I add a header while reading a CSV file into a Pandas DataFrame?

You can add a header while reading a CSV file into a Pandas DataFrame using the header parameter in the pd.read_csv() function. The header parameter allows you to specify which row of the CSV file should be used as the header for the DataFrame.

How do I add a header row to a DataFrame after creating it?

To add a header row to a DataFrame after creating it, you can use either the columns attribute or the rename method. For example, df.columns = ['Column1', 'Column2'] or df = df.rename(columns={'OldName': 'NewName'}) to assign or rename column names.

Can I add a header if my DataFrame is empty?

You can assign column names to an empty DataFrame using the columns attribute. However, with an empty DataFrame, it’s more common to set the columns during DataFrame creation.

What if my DataFrame has a MultiIndex? Can I still add a header?

If your DataFrame has a MultiIndex, you can use the set_names method to assign names to the levels of the MultiIndex, effectively adding a header. For example, df.set_names(['Level1', 'Level2'], inplace=True).

Conclusion

In this article, you have learned to add a header row to pandas DataFrame, while creating, when reading a CSV and to an existing DataFrame.

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