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!