<p>假设您有来自此程序的两个数据帧:</p>
<pre><code># Python 3.5.2
import pandas as pd
import numpy as np
# column names for example dataframe
cats = ['A', 'B', 'C', 'D', 'E']
df5 = pd.DataFrame(data = np.arange(25).reshape(5, 5), columns=cats)
print("Dataframe 5\n",df5,"\n")
df10=pd.DataFrame(data = np.transpose(np.arange(25).reshape(5, 5)), columns=cats)
print("Dataframe 10\n",df10)
</code></pre>
<p>产生的数据帧是:</p>
<pre><code>Dataframe 5
A B C D E
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
4 20 21 22 23 24
Dataframe 10
A B C D E
0 0 5 10 15 20
1 1 6 11 16 21
2 2 7 12 17 22
3 3 8 13 18 23
4 4 9 14 19 24
</code></pre>
<p>现在让我们看看第一次比较的结果:</p>
<pre><code>print(df5[-1:])
print(df10[-1:])
a=df10[-1:]< df5[-1:]
print("\n",a,"\n",type(a))
</code></pre>
<p>结果是:</p>
<pre><code> A B C D E
4 20 21 22 23 24
A B C D E
4 4 9 14 19 24
A B C D E
4 True True True True False
<class 'pandas.core.frame.DataFrame'>
</code></pre>
<p>第二个比较:</p>
<pre><code>print(df5[-2:-1])
print(df10[-2:-1])
b=df10[-2:-1]>df5[-2:-1]
print("\n",b,"\n",type(b))
</code></pre>
<p>结果是:</p>
<pre><code> A B C D E
3 15 16 17 18 19
A B C D E
3 3 8 13 18 23
A B C D E
3 False False False False True
<class 'pandas.core.frame.DataFrame'>
</code></pre>
<p><strong>问题:</strong></p>
<p>如果我们评估:</p>
<pre><code>pd.Series([True, True, False, False]) and pd.Series([False, True, False, True])
</code></pre>
<p>正确答案是什么?以下内容:</p>
<ol>
<li><code>pd.Series([False, True, False, False])</code></li>
<li><code>False</code></li>
<li><code>True</code></li>
<li>以上都是</li>
<li>以上任何一项</li>
<li>这取决于</li>
</ol>
<p>答案是:6-这要看情况而定。这取决于你想要什么。</em></p>
<p>首先,我们必须创建用于比较的布尔序列:</p>
<pre><code>a_new = (df10[-1:] < df5[-1:]).any()
print(a_new,"\n",type(a_new))
b_new = (df10[-2:-1] > df5[-2:-1]).any()
print("\n",b_new,"\n",type(b_new))
</code></pre>
<p>结果是:</p>
<pre><code>A True
B True
C True
D True
E False
dtype: bool
<class 'pandas.core.series.Series'>
A False
B False
C False
D False
E True
dtype: bool
<class 'pandas.core.series.Series'>
</code></pre>
<p>现在,我们可以计算3个案例。</p>
<p><strong>情况1:a.any()和b.any()</strong></p>
<pre><code>a.any() = True if any item in a is True
b.any() = True if any item in b is True
print(a_new.any() and b_new.any())
</code></pre>
<p>结果为<strong>真。</strong></p>
<p><strong>案例2:a.all()和b.all()</strong></p>
<pre><code>a.all() = True if every item in a is True
b.all() = True if every item in b is True
print(a_new.all() and b_new.all())
</code></pre>
<p>结果为<strong>假。</strong></p>
<p><strong>案例3:成对比较</strong></p>
<p>为此,必须将每个元素相互比较。</p>
<pre><code>result_pairwise = [a_new and b_new for a_new, b_new in zip(a_new,b_new)]
print(result_pairwise,"\n",type(result_pairwise))
</code></pre>
<p>结果是:</p>
<pre><code>[False, False, False, False, False]
<class 'list'>
</code></pre>
<p>有关详细信息:</p>
<ul>
<li><a href="http://pandas.pydata.org/pandas-docs/stable/gotchas.html#gotchas-truth/%22Pandas%20Gotchas%22" rel="nofollow noreferrer">Pandas Gotchas</a></li>
<li><a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#basics-compare" rel="nofollow noreferrer">Boolean Reductions.</a></li>
</ul>