I have a data frame with a large number of columns. In this question, however, I brought a mini-version of the df.
df = pd.DataFrame({'WT_IGL_x':[1,2,3,2,1,1,3,4,1,2], 'LA_WHN_x':[1,0,1,0,1,1,0,0,1,0], 'LA_WHN_y':[2,1,2,3,3,4,1,1,2,1], 'WT_IGL_y':[2,1,2,3,3,4,1,1,2,1], 'GT_OPP_IGL_x':[1,2,3,2,1,1,3,4,1,2], 'GT_OPP_IGL_y':[1,2,3,2,1,1,3,4,1,2]})
I want to subtract two columns that has same prefix and the middle part. In this case the 1st/4th columns and 2nd/3rd columns and add a column with the difference and name them by adding a suffix '_diff'
so my desired output will be:
df = pd.DataFrame({'WT_IGL_x':[1,2,3,2,1,1,3,4,1,2], 'LA_WHN_x':[1,0,1,0,1,1,0,0,1,0], 'LA_WHN_y':[2,1,2,3,3,4,1,1,2,1], 'WT_IGL_y':[2,1,2,3,3,4,1,1,2,1], 'WT_IGL_diff': [-1, 1, 1, -1, -2, -3, 2, 3, -1, 1], 'LA_WHN_diff': [-1, -1, -1, -3, -2, -3, -1, -1, -1, -1], GT_OPP_IGL_diff: [0,0,0,0,0,0,0,0,0,0]})
subtracting two columns is easy but
- matching two columns with the patterns
- adding columns with the differences
- name the column automatically with suffix '_diff'
are hard.
Thank you for your help.