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