Assign or add new column to dataframe in python pandas

In this tutorial we will learn how to assign or add new column to dataframe in python pandas.  assigning a new column the already existing dataframe in python pandas is explained with example. adding a new column the already existing dataframe in python pandas with an example

 

Create dataframe :

import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David'],
   'Score1':[62,47,55,74,31,77,85,63,42],
   'Score2':[89,87,67,55,47,72,76,79,44]}

df = pd.DataFrame(d)
print df

so the resultant dataframe will be

Assign or add new column to dataframe python pandas 1

 

Add or assign new column to existing dataframe in python pandas

To the above existing dataframe, lets add new column named Score3 as shown below


# assign new column to existing dataframe
df2=df.assign(Score3 = [56,86,77,45,73,62,74,89,71])
print df2

assign() function in python, assigns the new column to existing dataframe. So the resultant dataframe will be

Assign or add new column to dataframe python pandas 2

 

previous-small Assign or add new column to dataframe python pandas                                                                                                                next_small Assign or add new column to dataframe python pandas