繁体   English   中英

在 Pandas 中的不同列上使用 lambda dataframe

[英]Using lambda if condition on different columns in Pandas dataframe

我有简单的 dataframe:

import pandas as pd
frame = pd.DataFrame(np.random.randn(4, 3), columns=list('abc'))

因此例如:

a   b   c
0   -0.813530   -1.291862   1.330320
1   -1.066475   0.624504    1.690770
2   1.330330    -0.675750   -1.123389
3   0.400109    -1.224936   -1.704173

然后,如果 c 为正,我想创建包含来自“c”的值的“d”列。 来自“b”的其他值。

我在尝试:

frame['d']=frame.apply(lambda x: frame['c'] if frame['c']>0 else frame['b'],axis=0)

但是得到“ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at索引 a')

我试图谷歌如何解决这个问题,但没有成功。 有什么建议吗?

那是你要的吗?

In [300]: frame[['b','c']].apply(lambda x: x['c'] if x['c']>0 else x['b'], axis=1)
Out[300]:
0   -1.099891
1    0.582815
2    0.901591
3    0.900856
dtype: float64

使用矢量化方法

frame['d'] = frame.b + (frame.c > 0) * (frame.c - frame.b)

说明

这是从总和中得出的

(frame.c > 0) * frame.c  # frame.c if positive

(frame.c <= 0) * frame.b  # frame.b if c is not positive

然而

(frame.c <=0 )

相当于

(1 - frame.c > 0)

当你结合起来

frame['d'] = frame.b + (frame.c > 0) * (frame.c - frame.b)

我过来遇到了这样的事情,这就是我如何根据其他列的条件检索新列

df["col3"] = df[["col1", "col2"]].apply(
    lambda x: "return this if first statement is true"
    if (x.col1 == "value1" and x.col2 == "value2")
    else "return this if the statement right below this line is true"
    if (x.col1 == "value1" and x.col2 != "value2")
    else "return this if the below is true"
    if (x.col1 != "value1" and x.col2 == "Value2")
    else "return this because none of the above statements were true",
    axis=1
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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