I have already combined two different datasets into a DataFrame which looks like below.
| Date | ID | A_1 | A_2 | B_1 | B_2 | C_1 | C_2 |
|---|---|---|---|---|---|---|---|
| 01-01-2024 | 1 | 1 | 3 | 4 | 9 | 8 | 9 |
| 01-01-2024 | 3 | 9 | 8 | 7 | 4 | 3 | 2 |
| 01-01-2024 | 6 | 10 | 20 | 30 | 40 | 50 | 60 |
In the table above, columns A_1, B_1, C_1 are from dataset1 and columns A_2, B_2, C_2 are from dataset2.Now I want to calculate the absolute difference between the columns A_1, A_2, B_1, B_2, C_1, C_2 with the ID column as it is unique in both datasets.For that what I have so far is
columns = [A_1, A_2, B_1, B_2, C_1, C_2]for col in columns:diff = np.abs(df[f'{col}_1] - df[f'{col}_2])results_df[col] = [f"{round(diff)}"]The above code is giving me results with absolute difference between the columns, but I want to add ID column as first column. How can I do it?
I have added this to code which gave me the desired result which solved my issue:
results_df['DATE'] = df['DATE']results_df['ID'] = df['ID']