Create or add new column to dataframe in python pandas

In this section we will learn how to create or add new column to dataframe in python pandas.  creating a new column or variable to the already existing dataframe in python pandas is explained with example. adding a new column or variable to the already existing dataframe in python pandas with an example. Creating the new column has four different methods and adding a variable can be done by two different methods.

  • Create a new column in pandas python using assign function
  • Create a new variable in pandas python using dictionary
  • Create a new column to the particular position using insert() function
  • Create a new variable using list converted to column
  • Add New column based on existing column using  apply() function.

Create or add new column to dataframe in python pandas 14a

 

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

 

Create new column or variable 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, create the new column to existing dataframe. So the resultant dataframe will be

Assign or add new column to dataframe python pandas 2

 

 

Create a new variable using list converted to column in pandas:

To the above existing dataframe, lets add new column named “address” using list. As the list is created first and then added as the column to the dataframe as shown below

#### add a new column of the dataframe: Declare a list that is to be converted into a column

address = ['Newyork', 'California', 'Chennai', 'Vladivosk','London','Tokyo','Paris','Texas','Mumbai'] 
df['Address'] = address 

df

List is created first and then added to the dataframe as column, create the new column to existing dataframe using list is shown.

Create or add new column to dataframe in python pandas 11

 

 

Create a new variable to the particular position using insert() function in pandas python:

Create or add new column to dataframe in python pandas 14b

To the existing dataframe, lets add new column named “Address” to the mentioned position using insert() function. insert() function creates new column to the specific position as shown below.

#### Using DataFrame.insert() to add a column at specific position
df.insert(1, "Address", ['Newyork', 'California', 'Chennai', 'Vladivosk','London','Tokyo','Paris','Texas','Mumbai'] , True) 
df  

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

Create or add new column to dataframe in python pandas 12

 

 

Create a new variable through dictionary in pandas python:

To the existing dataframe, lets add new column named “address” using dictionary. As the dictionary is created as the column to the dataframe as shown below


#### add a new column of the dataframe: through dictionary

address = {'Newyork':'Alisa','California':'Bobby','Chennai':'Cathrine','Vladivosk':'Madonna','London':'Rocky','Tokyo':'Sebastian','Paris':'Jaqluine','Texas':'Rahul','Mumbai':'David'}
df['Address'] = address 
df

dictionary is created and then added to the dataframe as column, create the new column to existing dataframe using dictionary is shown.

Create or add new column to dataframe in python pandas 11

 

 

Add a new column in pandas python using existing column:

To the existing dataframe, lets add new column named “Total_score” using by adding “Score1” and “Score2”  using apply() function as shown below

 
#### new columns based on existing columns

df['Total_Score'] = df.apply(lambda row: row.Score1 + row.Score2, axis = 1) 
df

so the resultant dataframe will be

Create or add new column to dataframe in python pandas 13

 

 

Add a new column in pandas python using existing column:

To the existing dataframe, lets add new column named “Total_score” using by adding “Score1” and “Score2”  as shown below

 
#### new columns based on existing columns

df['Total_Score'] = df['Score1'] +  df['Score2']
df

so the resultant dataframe will be

Create or add new column to dataframe in python pandas 13

 

 


Other Related Topics :

Also you can refer here.

 

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

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts