In R and pandas, we can apply arithmetic operations in data columns in a similar manner. Hence, we can perform arithmetic operations such as addition or subtraction on elements in corresponding positions in two or more DataFrames.
Here, we construct a DataFrame in R with columns labeled x and y, and subtract column y from column x:
>norm_df<- data.frame(x=rnorm(7,0,1), y=rnorm(7,0,1)) >norm_df$x - norm_df$y [1] -1.3870730 2.4681458 -4.6991395 0.2978311 -0.8492245 1.5851009 -1.4620324
The with
operator in R also has the same effect as arithmetic operations:
>with(norm_df,x-y) [1] -1.3870730 2.4681458 -4.6991395 0.2978311 -0.8492245 1.5851009 -1.4620324
In pandas, the same arithmetic operations on columns can be done and the equivalent operator is eval
:
In [10]: import pandas as pd import numpy as np df = pd.DataFrame({'x': np.random.normal(0,1,size=7), 'y': np.random.normal(0,1,size=7)}) In [11]: df.x-df.y Out[11]: 0 -0.107313 1 0.617513 2 -1.517827 3 0.565804 4 -1.630534 5 0.101900 6 0.775186 dtype: float64 In [12]: df.eval('x-y') Out[12]: 0 -0.107313 1 0.617513 2 -1.517827 3 0.565804 4 -1.630534 5 0.101900 6 0.775186 dtype: float64