pandas: all NaNs when subtracting two dataframes
<p>I have two series. I want to subtract one dataframe from another dataframe, even though they have a different number of columns.</p>

<pre><code>&gt;df1

index   0   1   2    3    4    5
TOTAL   5  46  56  110  185  629

&gt;df2 
index   1   2   3    4    5
Use     25  37  86  151  512
</code></pre>

<p>I would assume that subtracting two dataframes with different dimensions would <em>only</em> result in NaNs in the mismatched columns (in this case, Column 0). The remaining columns would be the result of df1[1]-df2[1], df1[2]-df2[2], etc.</p>

<pre><code>&gt;df1 - df2
index   0    1   2   3   4   5
TOTAL   NaN  21  19  24  34  117
</code></pre>

<p>But this is not the case. This is what happens when I subtract the dataframes?</p>

<pre><code>&gt;df1 - df2
index   0   1   2   3   4   5
Use     NaN NaN NaN NaN NaN NaN
TOTAL   NaN NaN NaN NaN NaN NaN
</code></pre>

<p>I also tried just subtracting the values:</p>

<pre><code>&gt;df1.values - df2.values
Traceback (most recent call last):

  File "&lt;ipython-input-376-1dc5b3b4ad3e&gt;", line 1, in &lt;module&gt;
    total_drugs.values-(restraints_drugs.values+norestraints_drugs.values)

ValueError: operands could not be broadcast together with shapes (1,6) (1,5) 
</code></pre>

<p>What am I doing wrong? I'm using pandas 0.18.</p>

<p>You are subtracting two dataframes.  <strong><em>Both</em></strong> column and row indices must match.  In your case, the row indices <code>TOTAL</code> and <code>Use</code> do not match.</p>

<p>To get what you're looking for, you want to subtract the series <code>df2.ix['Use']</code> from <code>df1</code>  </p>

<pre><code>df1.sub(df2.squeeze())
</code></pre>

<p><a href="http://i.stack.imgur.com/oZWHC.png" rel="nofollow"><img src="http://i.stack.imgur.com/oZWHC.png" alt="enter image description here"></a></p>

<p>Or:</p>

<pre><code>df1.sub(df2.ix['Use'])
</code></pre>

<p>Or:</p>

<pre><code>df1.sub(df2.loc['Use'])
</code></pre>

<p>Or:</p>

<pre><code>df1 - df2.ix['Use']
</code></pre>

<p>Or:</p>

<pre><code>df1 - df2.loc['Use']
</code></pre>

python pandas