How to Add New column in DataFrame || Pandas DataFrame Insert() and assign()

How to Add New column in DataFrame

We can easily add a new column in the existing DataFrame. The following examples will clear you, on how to add new column in DataFrame.

#1. Method :- By adding a new list or Series

import pandas as pd
data = { ‘first’ : pd.Series ( [ 1, 2, 3 ] , index= [‘a’, ‘b’, ‘c’ ] ),
‘second’ : pd.Series ( [ 10, 20, 30 ], index= [‘a’, ‘b’, ‘c’ ] ) }
df= pd.DataFrame(data)
print (df)
print (“After adding a new column in Data Frame “)
df [ ‘third’ ]= pd.Series( [ 11, 22, 33 ], index= [‘a’, ‘b’, ‘c’ ] )
print (df)

Its output will be
        first        second
    一一一一一一一一一
a        1                10
b        2                20
c        3                30
After adding a new column in Data Frame
         first        second        third
     ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€
a        1                10            11
b        2                20            22
c        3                30            33
Note:- You can see it’s very easy to add a new column in DataFrame, You can add columns as much you want.

#2. Method :- By using DataFrame.insert() function

This method provides freedom to add a new column at any position, not just at the end.

import pandas as pd
data = { ‘first’ : pd.Series ( [ 1, 2, 3 ] , index= [‘a’, ‘b’, ‘c’ ] ),
‘second’ : pd.Series ( [ 10, 20, 30 ], index= [‘a’, ‘b’, ‘c’ ] ) }
df= pd.DataFrame(data)
print (df)
print (“By using insert() addition of a new column in Data Frame “)
df.insert(1,  “third ” ,  [ 11, 22, 33 ], True )
print (df)

Its output will be
        first        second
    一一一一一一一一一
a        1                10
b        2                20
c        3                30
By using insert() addition of a new column in Data Frame
         first        third           second
     ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€
a        1                11            10
b        2                22            20
c        3                33            30
Note:- By using insert(), we can add a new column in DataFrame at any position, not just at the end.

#3. Method :- By using DataFrame.assign() function

When we add a new column by using this method it will create a new DataFrame with a new column added to the old DataFrame.

import pandas as pd
data = { ‘first’ : pd.Series ( [ 1, 2, 3 ] , index= [‘a’, ‘b’, ‘c’ ] ),
‘second’ : pd.Series ( [ 10, 20, 30 ], index= [‘a’, ‘b’, ‘c’ ] ) }
df= pd.DataFrame(data)
print (df)
print (“To add a new column by using assign() it will create a new Data Frame “)
df2= df.assign(third =  [ 11, 22, 33 ] )
print (df2)

Its output will be
        first        second
    一一一一一一一一一
a        1                10
b        2                20
c        3                30
To add a new column by using assing() it will create a new DataFrame
         first        second        third
     ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€ä¸€
a        1                10            11
b        2                20            22
c        3                30            33
Note:- When we use assign(), to add a new column in Data Frame it will create a new DataFrame.
 If you like my post please share it with your friends by simply clicking the below social media button, and don’t forget to comment so we will update ourselves.
Share in a Single Click

Leave a Reply

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