Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For each row, how to sum values for columns start with same string

Edit: the column names indeed start with more than 1 character, but with a sep='_', it's more like AAA_BBB, AAA_DDD, BBB_EEE, BBB_FFF, ...

Thanks for the groupby solutions!


I have a pandas dataframe like this (borrowed from another question):

df =

C1    C2    T3  T5
28    34    11  22
45    100   33  66

How can I get a new dataframe, with sum of columns that have the same "starting string", e.g. "C", "T" ? Thanks!

df =

C     T  
62    33    
145   99

Unfortunately I have to deal with this structure of dataframe, and there are about 1000 columns in the dataframe, looks like A1,A2,A3,B1,B2,B3, ...

like image 250
user3275943 Avatar asked Dec 31 '22 17:12

user3275943


1 Answers

Use,

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

Output:

     C   T
0   62  33
1  145  99
like image 54
Scott Boston Avatar answered Feb 22 '23 07:02

Scott Boston