
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Creating a Pandas dataframe column based on a given condition in Python
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
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
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
- Related Articles
- Deleting a DataFrame row in Python Pandas based on column value
- Select rows from a Pandas DataFrame based on column values
- SUM a column based on a condition in MySQL
- Python – Create a new column in a Pandas dataframe
- Python - Stacking a multi-level column in a Pandas DataFrame
- Python - Add a zero column to Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe in Python
- Python - Calculate the variance of a column in a Pandas DataFrame
- Python - Add a prefix to column names in a Pandas DataFrame
- Python - How to select a column from a Pandas DataFrame
- Plot a multicolored line based on a condition in Python Matplotlib
- Python Pandas – Display all the column names in a DataFrame
- Python Pandas – Remove numbers from string in a DataFrame column
- Python – Center align column headers of a Pandas DataFrame
- How to export to PDF a graph based on a Pandas dataframe in Matplotlib?

Advertisements