'How to group column which respects certain conditions

I try since this afternoon to group column which respects certain conditions. I giva an easy example, I got 3 column like it :


ID1_column_A    ID2_column_B    ID2_column_C
 234               100              10
 334               130              11   
 34                250              40
 34                200              25

My aim is to group column who start per the same ID, so here, I will have only 2 column in output :

ID1_column_A        Fusion_B_C
 234                    110
 334                    141   
 34                     290
 34                     225

thanks for reading me



Solution 1:[1]

IIUC, you can try this:

df.groupby(df.columns.str.split('_').str[0], axis=1).sum()

Output:

   ID1  ID2
0  234  110
1  334  141
2   34  290
3   34  225

Solution 2:[2]

import pandas as pd

data = pd.DataFrame({'ID1_column_A': [234, 334, 34, 34], 'ID2_column_B': [100, 130, 250, 200], 'ID2_column_C': [10, 11, 40, 25]})
data['Fusion_B_C'] = data.loc[:, 'ID2_column_B':'ID2_column_C'].sum(axis=1)
data.drop(columns=['ID2_column_B', 'ID2_column_C'], inplace=True)#Deleting unnecessary columns
print(data)

Output

   ID1_column_A  Fusion_B_C
0           234         110
1           334         141
2            34         290
3            34         225

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scott Boston
Solution 2