简体   繁体   中英

How can I find the relative difference (percentage) but two data frames?

Given two data frames df1 and df2 , I can get the absolute difference between them via:

df3 = df1.compare(df2)

How can I get the relative difference between them?

I initially thought pct_change could do the job, but this function seems unrelated, since it doesn't take a data frame as input.

Assuming the shapes and dtypes are the same in both, try this:

import pandas as pd
import numpy as np

data_1 = {"a": [2, 4, 0, 6, 44], "b": [5, 3, 65, 33, 4]}

data_2 = {"a": [100, 4, 33, 32, 55], "b": [12, 55, 34, 76, 2]}

df_1 = pd.DataFrame.from_dict(data_1)
df_2 = pd.DataFrame.from_dict(data_2)

cols = []
for i, col_a in enumerate(df_1.columns):
    col_c_pct_change = (df_2.iloc[:, i] - df_1[col_a]) * 100 / df_1[col_a]
    cols.append(col_c_pct_change)

df3 = pd.concat(cols, axis=1, keys=[s.name for s in cols])

# replace infs with 0 (or whatever you preferred value is)
df3 = df3.replace([np.inf, -np.inf], np.nan).fillna(0)

             a            b
0  4900.000000   140.000000
1     0.000000  1733.333333
2     0.000000   -47.692308
3   433.333333   130.303030
4    25.000000   -50.000000

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