pandas,python中df.where()和df [(df [] ==)]之间的区别

D3V*_*LPR 4 python-3.x pandas

可以任何我帮助我说出pandas中这两个语句之间的区别 - python

df.where(df['colname'] == value)
Run Code Online (Sandbox Code Playgroud)

df[(df['colname'] == value)]
Run Code Online (Sandbox Code Playgroud)

为什么我在输出数据帧中获得不同的大小

Bou*_*oud 6

根据以下文件where:

返回与self相同形状的对象,其对应的条目来自self,其中cond为True,否则来自其他.

因此,where与使用括号过滤的目的略有不同,因为它会为您提供与您运行它的数据帧相同形状的结果.

目标是在文档的注释中:

where方法是if-then习语的应用.对于调用DataFrame中的每个元素,if cond是否使用True了元素; 否则other使用DataFrame中的相应元素