Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a conditional operation on a pandas column

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!

like image 361
A.K. Avatar asked Jun 19 '18 16:06

A.K.


People also ask

How do you use conditional statement in pandas?

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 conditional filtering method is possible on pandas?

Four main ways of conditional filtering in Pandas will be covered in this article: Pandas Boolean Indexing. Pandas Query. Pandas Eval.

How do you create a new column based on values from other columns in pandas?

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.


1 Answers

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
like image 161
sacuL Avatar answered Oct 05 '22 23:10

sacuL