English 中文(简体)
潘达斯的精准逻辑OR
原标题:Element-wise logical OR in Pandas

我知道并相应于<代码>&和NOT,~。 元素——合乎逻辑的OR操作者是什么? 我知道“或”本身不是我所期望的。

最佳回答

相应的操作员为>>。

 df[(df < 3) | (df == 5)]

如果价值低于3或等于5,则将进行要素检查。


If you need a function to do this, we have np.logical_or. For two conditions, you can use

df[np.logical_or(df<3, df==5)]

或者,在多种条件下使用<代码> 或.reduce/code>,

df[np.logical_or.reduce([df<3, df==5])]

由于这些条件被指定为个人论点,因此不需要 括号。

详情见

问题回答

采用以下两种系列的元件:<条码>a和<条码>。 公正

a | b

如果在单一数据组的栏目上运行,<代码>evalquery为<代码>或的选项。 工作要素——明智。 你们不需要担心母体会,因为比较操作者比养家/借方更优先。 例如,query 第A栏的数值与大于1,B栏的数值与2。

df = pd.DataFrame({ A : [1,2,0],  B : [0,1,2]})

df.query( A > 1 or B > 2 )       # == df[(df[ A ]>1) | (df[ B ]>2)]
#    A  B
# 1  2  1

or with eval you can return a boolean Series (again or works just fine as element-wise operator).

df.eval( A > 1 or B > 2 )
# 0    False
# 1     True
# 2    False
# dtype: bool
import pandas as pd

# Example with Series
s1 = pd.Series([True, False, True])
s2 = pd.Series([False, True, True])

result = s1 | s2

print(result)

<<>Output>:

0     True
1     True
2     True
dtype: bool

# Example with DataFrames

import pandas as pd


df1 = pd.DataFrame({ A : [True, False, True],  B : [False, True, False]})
df2 = pd.DataFrame({ A : [False, True, True],  B : [True, False, True]})

result = df1 | df2

print(result)

<<>Output>:

       A      B
0   True   True
1   True   True
2   True   True

操作员或关键词在《系列》或《数据框架》的相应内容之间用于执行要素——明智的逻辑性权利。 由此产生的系列或数据框架如果至少是输入系列或数据Frames中的相应内容之一,将具有真实性。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签