简体   繁体   中英

How to apply a function to a dataframe row based on a condition and values of another row?

If I have a pandas dataframe such as:

a   b   c  
1   2   3 
1   2  -3
2   3   2
4   2  -1

How do change the values of column b based on if the values in c are positive or negative, and use the values in b and a in the operation.

I want to run something like this on each row:

   if (c >= 0):
     b = a - b
   else:
     b = b - a 

and get the dataframe:

a   b   c  
1  -1   3 
1   1  -3
2  -1   2
4  -2  -1

You could use numpy.where which is similar to if/else and is usually faster:

  df.assign(b=np.where(df.c.ge(0), df.a - df.b, df.b - df.a))

    a    b   c
0   1   -1   3
1   1    1  -3
2   2   -1   2
3   4   -2  -1

Alternatively, you could use pandas' where method, which offers a similar approach:

 df.assign(b=df.a.sub(df.b).where(df.c.ge(0), df.b - df.a))

    a    b   c
0   1   -1   3
1   1    1  -3
2   2   -1   2
3   4   -2  -1

You can get the same result with pandas.DataFrame.apply :

df['b'] = df.apply(lambda x: x.a - x.b if x.c >= 0 else x.b - x.a, axis = 1)
#   a  b  c
#0  1 -1  3
#1  1  1 -3
#2  2 -1  2
#3  4 -2 -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM