My dataframe looks like this :
fruits = pd.DataFrame({'orange': [10, 20], 'apple': [30, 40], 'banana': [50, 60]})
apple banana orange
0 30 50 10
1 40 60 20
And I have this vector (its also a dataframe)
sold = pd.DataFrame({'orange': [1], 'apple': [2], 'banana': [3]})
apple banana orange
0 2 3 1
I want to subtract this vector to each row of the initial dataframe to obtain a dataframe which looks like this
apple banana orange
0 28.0 47.0 9.0
1 38.0 57.0 19.0
I tried :
print fruits.subtract(sold, axis = 0)
And the output is
apple banana orange
0 28.0 47.0 9.0
1 NaN NaN NaN
It worked only for the first line. I could create a dataframe filled with the vector for each row. Is there a more efficient way to subtract this vector ? I don't want to use a loop.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…