4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Add Header Row to Pandas DataFrame (With Examples)

How to Add Header Row to Pandas DataFrame (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use one of the following three methods to add a header row to a pandas DataFrame:

#add header row when creating DataFrame
df = pd.DataFrame(data=[data_values],
                  columns=['col1', 'col2', 'col3'])

#add header row after creating DataFrame
df = pd.DataFrame(data=[data_values])
df.columns = ['A', 'B', 'C']

#add header row when importing CSV
df = pd.read_csv('data.csv', names=['A', 'B', 'C'])

The following examples show how to use each of these methods in practice.

Example 1: Add Header Row When Creating DataFrame

The following code shows how to add a header row when creating a pandas DataFrame:

import pandas as pd
import numpy as np

#add header row when creating DataFrame 
df = pd.DataFrame(data=np.random.randint(0, 100, (10, 3)),
                  columns =['A', 'B', 'C'])

#view DataFrame
df

	A	B	C
0	81	47	82
1	92	71	88
2	61	79	96
3	56	22	68
4	64	66	41
5	98	49	83
6	70	94	11
7	1	6	11
8	55	87	39
9	15	58	67

Example 2: Add Header Row After Creating DataFrame

The following code shows how to add a header row after creating a pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame(data=np.random.randint(0, 100, (10, 3))) 

#add header row to DataFrame
df.columns = ['A', 'B', 'C']

#view DataFrame
df

	A	B	C
0	81	47	82
1	92	71	88
2	61	79	96
3	56	22	68
4	64	66	41
5	98	49	83
6	70	94	11
7	1	6	11
8	55	87	39
9	15	58	67

Example 3: Add Header Row When Importing DataFrame

The following code shows how to add a header row using the names argument when importing a pandas DataFrame from a CSV file:

import pandas as pd
import numpy as np

#import CSV file and specify header row names
df = pd.read_csv('data.csv', names=['A', 'B', 'C'])

#view DataFrame
df

	A	B	C
0	81	47	82
1	92	71	88
2	61	79	96
3	56	22	68
4	64	66	41
5	98	49	83
6	70	94	11
7	1	6	11
8	55	87	39
9	15	58	67

Related: How to Read CSV Files with Pandas

Additional Resources

How to Add Rows to a Pandas DataFrame
How to Add a Numpy Array to a Pandas DataFrame
How to Count Number of Rows in Pandas DataFrame

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories