Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If-else conditional assignment in pandas

I want to assign values to a column depending on the values of an already-existing column. This code works, but I would like to do it not-in-place, perhaps using assign or apply.

If this could be done in one step it would also avoid the implicit conversion from int to float that occurs below.

I've included my attempt using assign, which raises a ValueError.

import pandas as pd

original = pd.DataFrame({'col': ['a', 'b', 'c']})

d = original.copy()
d.loc[d.col.isin(['b', 'x']), 'new'] = 1
d.loc[~d.col.isin(['b', 'x']), 'new'] = 99
d

# :   col   new
# : 0   a  99.0
# : 1   b   1.0
# : 2   c  99.0

# original.assign(new=lambda x: (1 if x.col.isin(['b', 'x']) else 99)) # ValueError
like image 343
Hatshepsut Avatar asked Apr 10 '18 20:04

Hatshepsut


People also ask

How do pandas use conditional statements?

Applying an IF condition in Pandas DataFrame You then want to apply the following IF conditions: If the number is equal or lower than 4, then assign the value of 'True' Otherwise, if the number is greater than 4, then assign the value of 'False'

Is Iterrows faster than apply?

This solution also uses looping to get the job done, but apply has been optimized better than iterrows , which results in faster runtimes. See below for an example of how we could use apply for labeling the species in each row.


1 Answers

You can use numpy.where():

import numpy as np
original["new"] = np.where(original["col"].isin(["b", "x"]), 1, 99)
print(original)
#  col  new
#0   a   99
#1   b    1
#2   c   99
like image 102
pault Avatar answered Oct 24 '22 05:10

pault