3.1 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Add a Column to a Pandas DataFrame

How to Add a Column to a Pandas DataFrame

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 the assign() function to add a new column to the end of a pandas DataFrame:

df = df.assign(col_name=[value1, value2, value3, ...])

And you can use the insert() function to add a new column to a specific location in a pandas DataFrame:

df.insert(position, 'col_name', [value1, value2, value3, ...])

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23],
                   'assists': [5, 7, 7, 9, 12, 9],
                   'rebounds': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

        points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6
5	23	9	5

Example 1: Add New Column to End of DataFrame

The following code shows how to add a new column to the end of the DataFrame:

#add 'steals' column to end of DataFrame
df = df.assign(steals=[2, 2, 4, 7, 4, 1])

#view DataFrame
df

        points	assists	rebounds steals
0	25	5	11	 2
1	12	7	8	 2
2	15	7	10	 4
3	14	9	6	 7
4	19	12	6	 4
5	23	9	5	 1

Example 2: Add Multiple Columns to End of DataFrame

The following code shows how to add multiple new columns to the end of the DataFrame:

#add 'steals' and 'blocks' columns to end of DataFrame
df = df.assign(steals=[2, 2, 4, 7, 4, 1],
               blocks=[0, 1, 1, 3, 2, 5])

#view DataFrame
df

	points	assists	rebounds steals	blocks
0	25	5	11	 2	0
1	12	7	8	 2	1
2	15	7	10	 4	1
3	14	9	6	 7	3
4	19	12	6	 4	2
5	23	9	5	 1	5

Example 3: Add New Column Using Existing Column

The following code shows how to add a new column to the end of the DataFrame, based on the values in an existing column:

#add 'half_pts' to end of DataFrame
df = df.assign(half_pts=lambda x: x.points / 2)

#view DataFrame
df

        points	assists	rebounds half_pts
0	25	5	11	 12.5
1	12	7	8	 6.0
2	15	7	10	 7.5
3	14	9	6	 7.0
4	19	12	6	 9.5
5	23	9	5	 11.5

Example 4: Add New Column in Specific Location of DataFrame

The following code shows how to add a new column by inserting it into a specific location in the DataFrame:

#add 'steals' to column index position 2 in DataFrame
df.insert(2, 'steals', [2, 2, 4, 7, 4, 1])

#view DataFrame
df

        points	assists	steals	rebounds
0	25	5	2	11
1	12	7	2	8
2	15	7	4	10
3	14	9	7	6
4	19	12	4	6
5	23	9	1	5

Additional Resources

How to Change the Order of Columns in Pandas
How to Rename Columns in Pandas
How to Sort Columns by Name in Pandas

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