I know that this should be simple, but I want to take a column from a pandas dataframe, and for only the entries which meet some condition (say less than 1), multiply by a scalar (say 2).
For example, in this dataframe,
df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())
W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 0.651118 -0.319318 -0.848077 0.605965
C -2.018168 0.740122 0.528813 -0.589001
D 0.188695 -0.758872 -0.933237 0.955057
E 0.190794 1.978757 2.605967 0.683509
if I'm interested in carrying out this operation on column W
, the result should be
W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 1.302236 -0.319318 -0.848077 0.605965
C -4.036336 0.740122 0.528813 -0.589001
D 0.37739 -0.758872 -0.933237 0.955057
E 0.381588 1.978757 2.605967 0.683509
I have the below for an absolute assignment:
df.loc[df['W'] < 1, 'W'] = 4
but I'm not sure how to use the actual values from W
.
Thanks in advance!
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'
Four main ways of conditional filtering in Pandas will be covered in this article: Pandas Boolean Indexing. Pandas Query. Pandas Eval.
Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.
In your case, just use the *=
operator to make your multiplication in place:
If your Original dataframe looks like:
>>> df
W X Y Z
0 2.706850 0.628133 0.907969 0.503826
1 0.651118 -0.319318 -0.848077 0.605965
2 -2.018168 0.740122 0.528813 -0.589001
3 0.188695 -0.758872 -0.933237 0.955057
4 0.190794 1.978757 2.605967 0.683509
You can use:
df.loc[df['W'] < 1, 'W'] *= 2
resulting in this:
>>> df
W X Y Z
0 2.706850 0.628133 0.907969 0.503826
1 1.302236 -0.319318 -0.848077 0.605965
2 -4.036336 0.740122 0.528813 -0.589001
3 0.377390 -0.758872 -0.933237 0.955057
4 0.381588 1.978757 2.605967 0.683509
This is equivalent to the following:
df.loc[df['W'] < 1, 'W'] = df.loc[df['W'] < 1, 'W'] * 2
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