All Courses

How to create a new column in a data frame based on multiple conditions/other variables in Python?

By Manerushi149@gmail.com, a month ago
  • Bookmark
0

How to create a new column in a data frame based on multiple conditions/other variables in Python?

Create a new column
data frame
Multiple conditions
Variables
Python
1 Answer
0
Goutamp777

To create a new column in a data frame based on multiple conditions or other variables in Python, you can use the following steps:


  1. Import the required libraries and load the data into a data frame. For example, you can use pandas to load the data into a data frame:
import pandas as pd


data = pd.read_csv('data.csv')

2. Define the conditions that you want to use to create the new column. For example, let's say you want to create a new column called "Category" based on the values in the "Score" column:

conditions = [
    (data['Score'] >= 90),
    (data['Score'] >= 80) & (data['Score'] < 90),
    (data['Score'] >= 70) & (data['Score'] < 80),
    (data['Score'] >= 60) & (data['Score'] < 70),
    (data['Score'] < 60)
]


3. Define the values that you want to assign to the new column based on the conditions. For example, you can assign the values "A", "B", "C", "D", and "F" based on the conditions:

values = ['A', 'B', 'C', 'D', 'F']


4. Use the numpy select() function to create the new column based on the conditions and values:

import numpy as np


data['Category'] = np.select(conditions, values)


5. Finally, you can view the new column in the data frame:

print(data)


This will create a new column called "Category" in the data frame based on the values in the "Score" column and the defined conditions and values.


Your Answer

Webinars

How To Land a Job in Data Science?

Apr 6th (7:00 PM) 190 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More