Is there an easier way to find the percent difference between two dataframes.
For example:
df1((row1,col1) -df2(row1, col1))/average(df1(row1,col1), df2(row1,col1))
The picture shows the original dataframes, where I do it in a more manual way.
You can calculate the element-wise difference between two data frames like this:
diff_df = df1 - df2
The same way, you can add them together and divide them by 2. And multiply them by 100:
avg_df = (df1 + df2) / 2
You can divide diff_df
by avg_df
using .div()
. Multiplying that with 100 should get you what you need:
diff_df / avg_df * 100
You can also use the pandas methods to do this:
diff_df = df1.subtract(df2)
avg_df = df1.add(df2) / 2
diff_df.div(avg_df) * 100
Or, as a one-liner:
df1.subtract(df2).div(df1.add(df2).div(2)).mul(100)
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.