7

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!

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
A.K.
  • 73
  • 1
  • 3

2 Answers2

7

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
sacuL
  • 49,704
  • 8
  • 81
  • 106
3

You can also use numpy.where:

df['W'] = numpy.where(df['W'] < 1, df['W'] * 2, df['W'])

It checks for your condition and applies the relevant operation.

Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73