6 C
London
Tuesday, March 11, 2025
HomePandas in PythonGeneral Functions in PythonHow to Create a Duplicate Column in Pandas DataFrame

How to Create a Duplicate Column in 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 following basic syntax to create a duplicate column in a pandas DataFrame:

df['my_column_duplicate'] = df.loc[:, 'my_column']

The following example shows how to use this syntax in practice.

Example: Create Duplicate Column in Pandas DataFrame

Suppose we have the following pandas DataFrame:

import pandas as pd

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

#view DataFrame
print(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
6      25        9         9
7      29        4        12
8      32        5         8

We can use the following code to create a duplicate of the points column and name it points_duplicate:

#create duplicate points column
df['points_duplicate'] = df.loc[:, 'points']

#view updated DataFrame
print(df)

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

Notice that the points_duplicate column contains the exact same values as the points column.

Note that the duplicate column must have a different column name than the original column, otherwise a duplicate column will not be created.

For example, if we attempt to use the following code to create a duplicate column, it won’t work:

#attempt to create duplicate points column
df['points'] = df.loc[:, 'points']

#view updated DataFrame
print(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
6      25        9         9
7      29        4        12
8      32        5         8

No duplicate column was created.

The duplicate column must have a different column name than the original column.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Print Pandas DataFrame with No Index
How to Show All Rows of a Pandas DataFrame
How to Check dtype for All Columns 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