基于序列条件创建新的pandas列

2024-05-18 07:14:10 发布

您现在位置:Python中文网/ 问答频道 /正文

R到{},我似乎无法根据有条件地检查其他列来找出创建新列的简单情况。在

# In R, create a 'z' column based on values in x and y columns
df <- data.frame(x=rnorm(100),y=rnorm(100))
df$z <- ifelse(df$x > 1.0 | df$y < -1.0, 'outlier', 'normal')
table(df$z)
# output below
normal outlier 
     66      34 

尝试使用Python中的等效语句:

^{pr2}$

但是,将引发以下异常: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Python的方法是什么?非常感谢:)


Tags: columnsandindfoncreate情况column