column calculation based on column names Python

Question:

I have this dataframe with columns like

| LHA_1 | JH_1 | LHA_2 | JH_2 | LHA_3 | JH_3 | LHA_4 | JH_5 | ....

What I would like to do is to have LHA_2 - JH_1, LHA3 - JH_2, LHA4 - JH_3…..

and the final would look like

| LHA_1 | JH_1 | LHA_2 |LHA_2 - JH_1| JH_2 | LHA_3 |LHA_3 - JH_2| JH_3 | LHA_4 | JH_5 | ....
Asked By: Connie Xu

||

Answers:

You can use pd.IndexSlice. Suppose the following dataframe:

>>> df
   LHA_1  JH_1  LHA_2  JH_2  LHA_3  JH_3  LHA_4  JH_5  LHA_6
0      9     8      7     6      5     4      3     2      1
1     19    18     17    16     15    14     13    12     11
res = df.loc[:, pd.IndexSlice['LHA_2'::2]].values 
      - df.loc[:, pd.IndexSlice['JH_1'::2]].values
res = pd.DataFrame(res).add_prefix('LHA_JH_')
out = pd.concat([df, res], axis=1)
print(out)

# Output
   LHA_1  JH_1  LHA_2  JH_2  LHA_3  JH_3  LHA_4  JH_5  LHA_6  LHA_JH_0  LHA_JH_1  LHA_JH_2  LHA_JH_3
0      9     8      7     6      5     4      3     2      1        -1        -1        -1        -1
1     19    18     17    16     15    14     13    12     11        -1        -1        -1        -1
Answered By: Corralien
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.