Pandas Add Header Row to DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 17, 2023
Spread the love

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 that doesn’t have a header then 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.

1. Quick Examples to Add Header Row to pandas DataFrame

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


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

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

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

# 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. In order 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.


  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.

In the above sections, you have seen how to add while creating a DataFrame. Sometimes it’s not possible to know the headers up-front and you may need to add a header to the existing DataFrame.


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)

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

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.

Leave a Reply

You are currently viewing Pandas Add Header Row to DataFrame