Creating a Pandas dataframe column based on a given condition in Python

PythonServer Side ProgrammingProgramming

Pandas creates data frames to process the data in a python program. In this article we will see how we can add a new column to an existing dataframe based on certain conditions.

The Given Data Frame

Below is the given pandas DataFrame to which we will add the additional columns. It describes the Days and Subjects of an examination.

Example

 Live Demo

import pandas as pd

# Lists for Exam subjects and Days
Days = ['Mon', 'Tue', 'Wed','Thu', 'Fri']
Sub = ['Chemisry','Physics','Maths','English','Biology']

# Dictionary for Exam Schedule
Exam_Subjects = {'Exam Day': Days,
               'Exam Subject': Sub}

# Dictionary to DataFrame
Exam_Subjects_df = pd.DataFrame(Exam_Subjects)
print(Exam_Subjects_df)

Output

Running the above code gives us the following result −

   Exam Day  Exam Subject
0    Mon     Chemisry
1    Tue     Physics
2    Wed     Maths
3    Thu     English
4    Fri     Biology

Adding a New Column

Next we decide to add another column specifying the time of the exam. Here we add the condition using if statement and name the additional column as Time.

Example

 Live Demo

import pandas as pd

# Lists for Exam subjects
Days = ['Mon', 'Tue', 'Wed','Thu', 'Fri']
Sub = ['Chemisry','Physics','Maths','English','Biology']


# Dictionary for Exam Schedule
Exam_Subjects = {'Exam Day': Days,
                  'Exam Subject': Sub}

# Dictionary to DataFrame
Exam_Subjects_df = pd.DataFrame(Exam_Subjects)

Exam_Subjects_df['Time'] = ['2 PM' if x in('Mon','Thu') else '10 AM' for x in Exam_Subjects_df['Exam Day']]
print(Exam_Subjects_df)

Output

Running the above code gives us the following result −

  Exam Day   Exam Subject  Time
0  Mon       Chemisry      2 PM
1  Tue       Physics       10 AM
2  Wed       Maths         10 AM
3  Thu       English       2 PM
4  Fri       Biology       10 AM
raja
Published on 26-Aug-2020 06:48:15
Advertisements