Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Find absolute difference in Python using key columns

$
0
0

I have already combined two different datasets into a DataFrame which looks like below.

DateIDA_1A_2B_1B_2C_1C_2
01-01-20241134989
01-01-20243987432
01-01-20246102030405060

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

Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>