pandas keep both rows that meet or fail condition

Viewed 279

For a df that looks like this

d = {'age' : [21, 45, 45, 5],
'salary' : [20, 40, 10, 100]}

df = pd.DataFrame(d)

df
   age  salary
0   21      20
1   45      40
2   45      10
3    5     100

I am trying to add a column with boolean if condition is met

df['stat'] = df['salary'] < 40
df
   age  salary   stat
0   21      20   True
1   45      40  False
2   45      10   True
3    5     100  False

however when i assign the same condition to a variable i dont see true or false column

x1 = df['salary'] < 40
df[x1]
   age  salary
0   21      20
2   45      10

What is a best way to retain all rows but to add a column if a condition/s is met

If i have multiple condition something like below in

>>> x2 = df['age'] < 25
df[x1 & x2]
   age  salary
0   21      20

I would like to return all rows but with a stat column that would indicate T or F.

2 Answers

however when i assign the same condition to a variable i dont see true or false column

this is because with df[x1], pandas performs boolean indexing and you only end up with the rows that satisfy the condition.

retain all rows but to add a column if a condition/s is met

With your first way, you can chain multiple conditions and assign it alltogether to a new column instead of indexing with them:

>>> df["stat"] = x1 & x2
>>> df

   age  salary   stat
0   21      20   True
1   45      40  False
2   45      10  False
3    5     100  False

Either you can use apply function here:

df['stat'] = df.apply(lambda x: (x["age"] < 25) & (x["salary"] < 40),axis = 1)

or for better performance use np.where In np.where you can use any number of condition.

df['stat'] = np.where(df['age'] < 25,np.where(df['salary'] < 40,True,False),False)
Related