How to compare two rows of two pandas series?
<p>I have two python pandas series <code>df10</code> and <code>df5</code>. I want to compare their values.For example: <code>df10[-1:]&lt; df5[-1:]</code> ,it returns true. <code>df10[-2:-1] &gt; df5[-2:-1]</code> , it returns false.</p>

<p>But if I combine them together, <code>df10[-1:]&lt; df5[-1:] and df10[-2:-1]&gt;df5[-2:-1]</code>,it returns </p>

<blockquote>
  <p>The truth value of a Series is ambiguous. Use a.empty, a.bool(),
  a.item(), a.any() or a.all()</p>
</blockquote>

<p>But I expect it returns false. How can I solve this problem?</p>

<p>Consider you have the two dataframes from this program:</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>The resulting data frames are:</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>Now let's look at the result of your first comparison:</p>

<pre><code>print(df5[-1:])
print(df10[-1:])

a=df10[-1:]&lt; df5[-1:]

print("\n",a,"\n",type(a))
</code></pre>

<p>which results in:</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 
 &lt;class 'pandas.core.frame.DataFrame'&gt;
</code></pre>

<p>Now the second comparison:</p>

<pre><code>print(df5[-2:-1])
print(df10[-2:-1])

b=df10[-2:-1]&gt;df5[-2:-1]
print("\n",b,"\n",type(b))
</code></pre>

<p>which has results:</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 
 &lt;class 'pandas.core.frame.DataFrame'&gt;
</code></pre>

<p><strong>The issue:</strong></p>

<p>If we evaluate: </p>

<pre><code>pd.Series([True, True, False, False]) and pd.Series([False, True, False, True])
</code></pre>

<p>What is the correct answer?:</p>

<ol>
<li>pd.Series([False, True, False, False])</li>
<li>False</li>
<li>True</li>
<li>All of the above</li>
<li>Any of the above</li>
<li>It depends</li>
</ol>

<p>The answer is: 6 - It depends. It depends on <em>what you want.</em></p>

<p>First, we have to create boolean series for the comparison:</p>

<pre><code>a_new = (df10[-1:] &lt; df5[-1:]).any()
print(a_new,"\n",type(a_new))

b_new = (df10[-2:-1] &gt; df5[-2:-1]).any()
print("\n",b_new,"\n",type(b_new))
</code></pre>

<p>The results are:</p>

<pre><code>A     True
B     True
C     True
D     True
E    False
dtype: bool 
 &lt;class 'pandas.core.series.Series'&gt;

A    False
B    False
C    False
D    False
E     True
dtype: bool 
 &lt;class 'pandas.core.series.Series'&gt;
</code></pre>

<p>Now, we can compute 3 cases.</p>

<p><strong>Case 1: a.any() and b.any()</strong></p>

<p>a.any() = True if any item in a is True</p>

<p>b.any() = True if any item in b is True</p>

<pre><code>print(a_new.any() and b_new.any())
</code></pre>

<p>The result is <strong>True.</strong></p>

<p><strong>Case 2: a.all() and b.all()</strong></p>

<p>a.all() = True if every item in a is True</p>

<p>b.all() = True if every item in b is True</p>

<pre><code>print(a_new.all() and b_new.all())
</code></pre>

<p>The result is <strong>False.</strong></p>

<p><strong>Case 3: Pairwise comparison</strong></p>

<p>For this, you have to compare every element to each other. </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>The result is:</p>

<pre><code>[False, False, False, False, False] 
 &lt;class 'list'&gt;
</code></pre>

<p>For more details, see the <a href="http://pandas.pydata.org/pandas-docs/stable/gotchas.html#gotchas-truth/%22Pandas%20Gotchas%22" rel="nofollow">Pandas Gotchas</a> and <a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#basics-compare" rel="nofollow">Boolean Reductions.</a></p>

python pandas