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
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'
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With