Learn to insert a column in Python based on condition – Pandas

This tutorial will teach us how to insert a new column based on conditions in Python. It is easy to add a column in Python. But here we will add a column with values based on some conditions. We can do it manually for small datasets but for large datasets, we use libraries and a few lines of code.

How to proceed:

Step 1: Import the required libraries

Step 2: Creating the dataset

Step 3: Write conditions to find values of the column

Step 4: Insert the list into the dataset and display

Step 1: Import the required libraries

import pandas as pd
import numpy as np

The required libraries Pandas and NumPy are imported. There is no need for any other libraries.

Step 2: Creating the dataset

Let’s create a data frame called ‘student’ with columns ‘student_name’ and ‘student_score’ as columns. We use print statement to view our dataset.

data={'student_name':['Harish','Jack','Satish','Aarna','Diya','Navya'],'student_score':[557,515,490,525,564,531]}
student=pd.DataFrame(data,columns=['student_name','student_score'])
print(student)

Output:

  student_name  student_score
0       Harish            557
1         Jack            515
2       Satish            490
3        Aarna            525
4         Diya            564
5        Navya            531

Step 3: Write conditions to find values of the column

Here, we create an empty list called ‘grade’ in which we store the values of the column. Now, we use a for loop with if-elif-else condition to assign grades to each student individually based on conditions.

The conditions are as follows:

  • if the value of ‘student_score’ is greater than 550 then it will assign ‘O’ in the grade column
  • if the value of ‘student_score’ is between 500 and 550 then it will assign ‘A’ in the grade column
  • if the value of ‘student_score’ is between 450 and 500 then it will assign ‘B’ in the grade column
  • if the value of ‘student_score’ is between 400 and 450 then it will assign ‘C’ in the grade column
  • if the value of ‘student_score’ is between 350 and 400 then it will assign ‘D’ in the grade column
  • if the value of ‘student_score’ is less than 350 then it will assign ‘F’ in the grade column

 

grade = []
for row in student['student_score']:
    if row >550 :    grade.append('O')
    elif row >500:   grade.append('A')
    elif row >450:   grade.append('B')
    elif row >400:   grade.append('C')
    elif row >350:   grade.append('D')
    else:            grade.append('F')

Step 4: Insert the list into the dataset and display

Finally, we are inserting the list with values as a column into dataset. Now, print the resultant dataset to see the changes.

student['grade']=grade
print(student)

Output:

  student_name  student_score grade
0       Harish            357     D
1         Jack            515     A
2       Satish            490     B
3        Aarna            525     A
4         Diya            564     O
5        Navya            431     C

 

You may also learn,

Leave a Reply

Your email address will not be published.