I have a pandas dataframe with 600 columns (df1), and I want to sum the values of each column in groups of 6. In other words, I want to create a new dataframe (df2) that has 100 columns, each column being the sum of 6 columns from the input dataframe. For example, Each row the first column in df2 will be the sum of the first six columns in df1 (keeping the rows separate). The dataframe I am using also has string values for each column name (here just represented with single letters)
For df1:
A B C D E F G H I J ...
0 9 6 3 4 7 7 6 0 5 2 ...
1 8 0 6 6 0 5 6 5 8 7 ...
2 9 0 7 2 9 5 3 2 1 7 ...
3 5 2 9 6 7 0 3 8 5 0 ...
4 7 1 0 7 4 0 2 0 5 8 ...
5 0 9 2 0 4 9 5 7 6 2 ...
I would want the first column of df2 to be:
A G ...
0 36
1 25
2 32
3 29
4 19
5 24
Where each row is the sum of the first six columns of that row. The next column would then be the sum of the next six columns and so on, with the column name being the name of the first column in each set of 6. (First column name is the first column's, the second column name is the seventh column's, etc.)
I've tried using the column indices to sum the correct columns, but I am having issues finding a way to store the sums in new columns with relevant names.
Is there a pythonic way to create these columns, and pull column names from df into df2?