df.where() 和 df [ (df [ ] == ) ] 在 Pandas 中的区别,python [英] Difference between df.where( ) and df [ (df [ ] == ) ] in pandas , python
问题描述
我能帮我说出熊猫中这两个语句之间的区别吗-python
Can Any I help me in telling the difference between these two statements in pandas - python
df.where(df['colname'] == value)
和
df[(df['colname'] == value)]
为什么我在输出数据框中得到不同的大小
Why Am I getting different sizes in the output dataframe
推荐答案
根据 where
的文档:
返回一个与 self 形状相同的对象及其对应的条目来自 self,其中 cond 为 True,否则来自 other.
Return an object of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other.
所以 where
的目的与用括号过滤略有不同,因为它会为您提供与运行它的数据帧形状相同的结果.
So the purpose of where
is 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 方法是 if-then 习语的应用.对于每个调用 DataFrame 中的元素,如果 cond
是 True
元素用来;否则来自 DataFrame 的相应元素other
使用
The where method is an application of the if-then idiom. For each element in the calling DataFrame, if
cond
isTrue
the element is used; otherwise the corresponding element from the DataFrameother
is used
这篇关于df.where() 和 df [ (df [ ] == ) ] 在 Pandas 中的区别,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!