[英]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:如果我有一个 pandas dataframe 例如:
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.如何根据 c 中的值是正数还是负数来更改 b 列的值,并在操作中使用 b 和 a 中的值。
I want to run something like this on each row:我想在每一行上运行这样的东西:
if (c >= 0):
b = a - b
else:
b = b - a
and get the dataframe:并获得 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:您可以使用numpy.where ,它类似于if/else
并且通常更快:
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:或者,您可以使用 pandas 的where方法,它提供了类似的方法:
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
:您可以使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.