Add Column at a specific position in Pandas DataFrame

In this article we will discuss different ways to add a column at a specific position in Pandas DataFrame in Python.

Table Of Contents

Preparing DataSet

First we will create a DataFrame from list of tuples i.e.

import pandas as pd

# List of Tuples
employees= [('Mark', 'US', 'Tech',   5),
            ('Riti', 'India', 'Tech' ,   7),
            ('Shanky', 'India', 'PMO' ,   2),
            ('Shreya', 'India', 'Design' ,   2),
            ('Aadi', 'US', 'Tech', 11),
            ('Sim', 'US', 'Tech', 4)]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(employees,
                  columns=['Name', 'Location', 'Team', 'Experience'])
print(df)

Output:

     Name Location    Team  Experience
0    Mark       US    Tech           5
1    Riti    India    Tech           7
2  Shanky    India     PMO           2
3  Shreya    India  Design           2
4    Aadi       US    Tech          11
5     Sim       US    Tech           4

Now we will learn about different ways to add a column at a sepcific position in this DataFrame.

Advertisements

Method 1: Using insert() function

The DataFrame provides a function insert(), and it accepts following arguments,

  • The position, at which the column needs to be added.
  • The Name of the new column, that will be inserted.
  • Values that needs to be added in the new column.

It will add a column with given values, at the specified position in the DataFrame. If a single value is provided as third argument, then all the values in the newly added column will be equal to that only.

Let’s see some examples,

Example 1: Add a new column as second column in DataFrame, but with similar values

We will add a new column ‘Bonus1’ in the DataFrame at position 1 i.e. as the second column of the DataFrame. Therefore, we will pass following arguments in the insert() function,

  • 1 as the position index, at which column needs to be added
  • Name of the new column
  • Value 40. It means all values in new column will be 40.

Let’s see the example,

# Insert a column at poistion 1 i.e. as the 2nd column of DataFrame
# with same values
df.insert(1, "Bonus1", 40)

print(df)

Output:

     Name  Bonus1 Location    Team  Experience
0    Mark      40       US    Tech           5
1    Riti      40    India    Tech           7
2  Shanky      40    India     PMO           2
3  Shreya      40    India  Design           2
4    Aadi      40       US    Tech          11
5     Sim      40       US    Tech           4

It added a new column ‘Bonus1’ at position 1 i.e. as second column in DataFrame. All the values in column are same i.e. 40.

Example 2: Add new column as second column with different values

We will add a new column ‘Bonus2’ in the DataFrame at position 1 i.e. as the second column of the DataFrame. Therefore, we will pass following arguments in the insert() function,

  • 1 as the position index, at which column needs to be added
  • The name of new column.
  • A List containing the values of new column. List size is equal to the number of rows in the column.

Let’s see the example,

listOfValues = [31, 33, 56, 43, 41, 31]

# Insert a column at poistion 1 i.e. as the 2nd column of DataFrame
# with different values
df.insert(1, "Bonus2", listOfValues, allow_duplicates=False)

print(df)

Output:

     Name  Bonus2  Bonus1 Location    Team  Experience
0    Mark      31      40       US    Tech           5
1    Riti      33      40    India    Tech           7
2  Shanky      56      40    India     PMO           2
3  Shreya      43      40    India  Design           2
4    Aadi      41      40       US    Tech          11
5     Sim      31      40       US    Tech           4

It added a new column ‘Bonus2’ at position 1 i.e. as the second column in DataFrame.

Method 2: Using [] Operator

Add new column using the [] operator of DataFrame i.e.

df['New_column_name'] = listOfValues

using this, we can add a column ‘Bonus3’ in the end of DataFrame i.e. as the last column of DataFrame. Then we can change the order of columns in DataFrame to make this new column ‘Bonus3’ as the second column of dataFrame. Let’s see the example,

# Add new column in DataFrame
df['Bonus3'] = [32, 32, 56, 43, 42, 37]

# Change the position of columns to make column 'Bonus3' as the second column
df = df[['Name','Bonus3','Bonus2', 'Bonus1','Location','Team', 'Experience']]

print(df)

Output:

     Name  Bonus3  Bonus2  Bonus1 Location    Team  Experience
0    Mark      32      31      40       US    Tech           5
1    Riti      32      33      40    India    Tech           7
2  Shanky      56      56      40    India     PMO           2
3  Shreya      43      43      40    India  Design           2
4    Aadi      42      41      40       US    Tech          11
5     Sim      37      31      40       US    Tech           4

It added a new column ‘Bonus3’ at position 1 i.e. as the second column in DataFrame.

Summary

We learned about different ways to add a column at a specific position in Pandas DataFrame. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top