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, ...
Use,
df.groupby(df.columns.str[0], axis=1).sum()
Output:
C T
0 62 33
1 145 99
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With