0

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')

enter image description here

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:

enter image description here

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.

  • You want to resample the data to the same x-coordinates, https://stackoverflow.com/questions/29085268/resample-a-numpy-array But you'll have to think about what the difference is supposed to mean if the domain isn't the same for F1 and F2 as in the last plot. – Mikael Öhman Oct 02 '22 at 23:06

1 Answers1

0
shift = 2
x_new = pd.concat([f1.x, pd.Series(list(range(last_x + 1, last_x + shift + 1)))]).reset_index(drop=True)
f1_y_new = pd.concat([f1.y, pd.Series([0] * shift)]).reset_index(drop=True)
f2_y_new = pd.concat([pd.Series([0] * shift), f2.y]).reset_index(drop=True)
f_diff = pd.DataFrame({'x': x_new, 'y': abs(f1_y_new-f2_y_new)})