df.where( ) 和 df [ (df [ ] == ) ] 在 Pandas 中的区别,python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40332974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:18:56  来源:igfitidea点击:

Difference between df.where( ) and df [ (df [ ] == ) ] in pandas , python

python-3.xpandas

提问by D3VLPR

Can Any I help me in telling the difference between these two statements in pandas - python

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

df.where(df['colname'] == value)

and

df[(df['colname'] == value)]

Why Am I getting different sizes in the output dataframe

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

回答by Boud

As per the documentation of where:

根据以下文档where

Return an object of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other.

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

So the purpose of whereis slightly different than filtering with brackets, as it will give you the result with the same shape of the dataframe you run it against.

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

The goal is in the notes of the documentation:

目标在文档的注释中:

The where method is an application of the if-then idiom. For each element in the calling DataFrame, if condis Truethe element is used; otherwise the corresponding element from the DataFrame otheris used

where 方法是 if-then 习语的应用。对于在主叫数据帧的每个元素中,如果condTrue的元件被使用; 否则other使用DataFrame 中的相应元素