How to populate a dataframe column based on condition met in another column

Jabbar

I was wondering how for the following dataframe can make a new column (for example B), and for each row define if its value is A1: x>2, A2: between(2,0), A3: between(0,-2), or A4: x<-2.

imprt pandas as pd
imort numpy as np
df = pd.DataFrame({'A':[-4,-3.5,-2.5,-1,1,1.5,2,2.5,3.5]})

I tried the following code but it didnt work.

df['B'] = np.where((df['A']>2), 'A1',
               np.where(df['A'].between(2,0),'A2',
                         np.where(df['A'].between(0,-2),'A3', 
                                   np.where(df['A']<-2), 'A4'))
jezrael

Your solution is possible if change ():

df['B'] = np.where(df['A']>2,'A1',
          np.where(df['A'].between(0,2),'A2',
          np.where(df['A'].between(-2,0),'A3', 
          np.where(df['A']<-2, 'A4',''))))

Alternative with cut:

df['B1'] = pd.cut(df['A'], bins=(-np.inf,-2,0,2,np.inf), labels=('A4','A3','A2','A1'))
print (df)
     A   B  B1
0 -4.0  A4  A4
1 -3.5  A4  A4
2 -2.5  A4  A4
3 -1.0  A3  A3
4  1.0  A2  A2
5  1.5  A2  A2
6  2.0  A2  A2
7  2.5  A1  A1
8  3.5  A1  A1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R - Add values (derived by a formula) in a dataframe column based on a condition met by values in a column of another dataframe

filter based on condition met in another column

How to find minimum value in a column based on condition in an another column of a dataframe?

How to add a new column based another column condition in pandas dataframe

how to add column in dataframe based on some condition from another column?

How to write value from a dataframe column to another column based in a condition?

Pandas - populate one dataframe column based on another

How to match column value in a dataframe based on condition in another dataframe?

How to replace the values in a dataframe column based on another dataframe condition

How to fetch a column header if a particular condition is met based on row and column value of the dataframe?

How to transpose a column based on the condition in another column?

Populate a panda's dataframe column based on another column and dictionary value

How to sum cells in a column if a condition is met in another column in SQL

Filter a dataframe based on a condition of another column

Fill a column in a dataframe if a condition is met

Exclude rows from dataframe except if another column condition met

Polars: change a value in a dataframe if a condition is met in another column

assign values of one dataframe column to another dataframe column based on condition

Changing date column if condition is met in another column

Pandas populate new dataframe column based on matching columns in another dataframe

How to slice a dataframe column based on another column

Updating a column in a Pandas DataFrame based on a condition of another column

extract a column in dataframe based on condition for another column R

Populate a dataframe column based on a column of other dataframe

How do I populate a column based on whether it matches another column or not?

How to fill the value into a new column based on the condition of another column in Pandas dataframe?

Populate column in data frame based on a range found in another dataframe

Populate column in `Pandas.DataFrame` based on matches in another columns

How to reset a groupby cumsum when a condition in another column is met?