Assume you have two DataFrames f1 and f2, with columns x, and y. We want to calculate an absolute difference between those two functions abs(y1-y2).
f1 = pd.DataFrame({'x': range(1, 6), 'y': range(10, 0, -2)})
f2 = pd.DataFrame({'x': range(1, 6), 'y': range(10, 5, -1)})
f_diff = pd.DataFrame({'x': range(1, 6), 'y': abs(f1.y-f2.y)})
plt.style.use('default')
fig, axs = plt.subplots(figsize=(10, 5), facecolor='w', edgecolor='k')
axs.plot(f1.x, f1.y, color='orange')
axs.plot(f2.x, f2.y, color='red')
axs.plot(f_diff.x, f_diff.y, color='black')
Now, I would like to shift one of the functions to the left or right (apply vector transformation along X axis), and re-calcualte the absolute difference. See below:
So my question: How to calculate the difference between two columns (Ys) on locations where x values in two data frames are the same? Obvioulsy we can't use a trivial abs(f1.y-f2.y) anymore. I was thinking about shifiting values in one of the y columns but I was wondering if there is more efficient solution.