简体   繁体   中英

Calculate difference between two columns where row values are the same (difference between 2 functions)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM