Pandas:使用where()方法与另一个系列?



我知道pandas中的where()方法的含义。但是在手册中,我不明白他们用

检查什么

t = pd。系列([真,假])

有人能帮帮忙吗?下面是代码:

s = pd.Series(range(5))
t = pd.Series([True, False])
s.where(t, 99)
0     0
1    99
2    99
3    99
4    99

我将创建一个数据框架来帮助您理解

s.to_frame('s').assign(t=t, s_where1=s.where(t), s_where2=s.where(t, 99))

结果:

s   t       s_where1    s_where2
0   0   True    0.0         0            <-- if t is True, remain value of s
1   1   False   NaN         99           <-- When not True, 
2   2   NaN     NaN         99               where function  produce NaN in where1. 
3   3   NaN     NaN         99               where function fills NaN with 99 in where2
4   4   NaN     NaN         99               



如果条件的大小与原级数相同,函数where不重要。认为where函数变化除了True

最新更新