Pandas: all NaN when subtracting two data frames

I have two series. I want to subtract one data frame from another data frame, even if they have a different number of columns.

>df1

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

>df2 
index   1   2   3    4    5
Use     25  37  86  151  512

I would suggest that subtracting two data frames with different dimensions will only result in NaNs in inconsistent columns (in this case, column 0). The remaining columns will be the result of df1 [1] -df2 [1], df1 [2] -df2 [2], etc.

>df1 - df2
index   0    1   2   3   4   5
TOTAL   NaN  21  19  24  34  117

But this is not so. Is this what happens when I subtract dataframes?

>df1 - df2
index   0   1   2   3   4   5
Use     NaN NaN NaN NaN NaN NaN
TOTAL   NaN NaN NaN NaN NaN NaN

I also tried just subtracting the values:

>df1.values - df2.values
Traceback (most recent call last):

  File "<ipython-input-376-1dc5b3b4ad3e>", line 1, in <module>
    total_drugs.values-(restraints_drugs.values+norestraints_drugs.values)

ValueError: operands could not be broadcast together with shapes (1,6) (1,5) 

What am I doing wrong? I am using pandas 0.18.

+4
source share
1 answer

. . TOTAL Use .

, , df2.ix['Use'] df1

df1.sub(df2.squeeze())

enter image description here

:

df1.sub(df2.ix['Use'])

:

df1.sub(df2.loc['Use'])

:

df1 - df2.ix['Use']

:

df1 - df2.loc['Use']
+2

Source: https://habr.com/ru/post/1652624/


All Articles