1

I got two DataFrames and Want to substract.

df1:

              Val1         Val2      Val3       
0  -27 -0.8  -6.786321    -7.024615 -13.946589  
1  -27 -0.9  -5.746795    -5.804550 -11.576365  
2  -27 -1.0  -4.624857    -4.372321  -9.103681  
3  -27 -1.2  -2.685832    -2.418888  -5.057056  
4  -27 -1.4  -1.445561    -1.389468  -2.622357  

df2:

       Bench
0      0.4601
1     -5.3336
2     -6.0163
3     -4.1776
4     -2.3472

As I have the same indexes, I tried to do: df1-df2, but it didn't work.

Therefore I've tried to use another way:

   headers = list(df1.columns.values)
   filtr_headers = filter(lambda x: x!='',headers)
         for i in filtr_headers:
                df1['%s' %(i)] = df1['%s' %(i)] - df2['Bench']

But I'm getting in return Dataframe with NaN values. I don't know why it's happening. Any hints will be higly appreciated.

0

2 Answers 2

1

You can use pd.DataFrame.sub, like this:

In [113]: df1.sub(df2.Bench.values, axis=0)
Out[113]: 
                Val1      Val2       Val3
0 -27 -0.8 -7.246421 -7.484715 -14.406689
1 -27 -0.9 -0.413195 -0.470950  -6.242765
2 -27 -1.0  1.391443  1.643979  -3.087381
3 -27 -1.2  1.491768  1.758712  -0.879456
4 -27 -1.4  0.901639  0.957732  -0.275157
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! It works out! Though I try to understand why my way of implementing is not working. s the problem with a different numbers of columns in these two Dataframes? I also want to skip two firs columns while subtracting. I've tried to do it by: df1.ix['Val1':].sub(df2.Bench.values, axis=0), but I'm getting error again.
@Monica You're welcome. About your first point - it attempts to do a join to subtract same-name columns, but there aren't any. About your second point - you have a strange index thing going on. Could I ask you to open a separate question about it? It's a separate question, and interesting in its own right. If you want, drop here a link to it, and I'll be happy to write there.
@Monica In any case, those are not columns, they're part of the index. Try ` df1.index = range(5)` and see what happens.
Thank you again for your explanation. You're totally right, they are indexes. btw Is there any possibility to talk to you on a private?
|
0

You can use

pd.DataFrame(df1.values - df2.values)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.