How to add header row to a pandas DataFrame

  1. Add header row by passing directly it in dataframe method
  2. Add header row using dataframe.columns
  3. header adding without replacing current header
  4. Add header row adding to dataframe when reading csv files

We will introduce the method to add a header row to a pandas dataframe, and options like by passing names directly in the dataframe or by assigning directly the column names in a list to the dataframe.columns method.

We will also introduce the Pandas dataframe add header without replacing the current header. In other words, we will just shift the current header down and just add it to the dataframe as another record.

We will also look at the example of how to add a header row to a dataframe while reading csv files.

Add header row by passing directly it in dataframe method

We will pass header directly to dataframe by using columns argument.

Consider the following code:

# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(
    data=np.random.randint(
        0, 10, (6,4)),
    columns =["a", "b", "c", "d"])
print(df)

Output:

   a  b  c  d
0  4  4  4  0
1  8  1  2  5
2  3  0  4  3
3  3  7  2  4
4  8  3  1  8
5  6  7  5  9

Add header row using dataframe.columns

We can also add header row to dataframe by using dataframe.columns.

Consider the following code:

# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(
    data=np.random.randint(
        0, 10, (6,4)))
df.columns=["a", "b", "c", "d"]
print(df)

Output:

   a  b  c  d
0  5  2  6  7
1  4  5  9  0
2  8  3  0  4
3  6  3  1  1
4  9  3  4  8
5  7  5  0  6

header adding without replacing current header

Another option is to add header row as an additional level of the column index, to make it a MultiIndex. This approach is helpful when we need an extra layer of information for columns.

Consider the following code:

# python 3.x
import pandas as pd
import numpy as np
df = pd.DataFrame(
    data=np.random.randint(
        0, 10, (6,4)),
    columns=["a", "b", "c", "d"])
df.columns = pd.MultiIndex.from_tuples(
    zip(['A', 'B','C', 'D'], 
        df.columns))
print(df)

Output:

   A  B  C  D
   a  b  c  d
0  2  6  4  6
1  5  0  5  1
2  9  6  6  1
3  8  9  7  4
4  6  5  6  6
5  3  9  1  5

Add header row adding to dataframe when reading csv files

We can use names directly in the read_csv or we can set header=None explicitly if a file has no header.

Consider the following code:

# python 3.x
import pandas as pd
import numpy as np
df = pd.Cov = pd.read_csv(
    "path/to/file.csv", 
     sep='\t', 
     names=["a", "b", "c", "d"])

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to delete a row based on column value in Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • comments powered by Disqus