Skip to content
Advertisement

GroupBy columns on column header prefix

I have a dataframe with column names that start with a set list of prefixes. I want to get the sum of the values in the dataframe grouped by columns that start with the same prefix.

JavaScript

The only way I could figure out how to do it was to loop through the prefix list, get the columns from the dataframe that startwith that string, and then sum the results.

JavaScript

I hoped there was a more elegant way to do it, perhaps with groupby(), but I couldn’t figure it out.

Advertisement

Answer

First, it is necessary to determine what columns contain what prefix. We then use this to perform a groupby.

JavaScript

Almost there, now,

JavaScript

Another option is using np.char.startswith and argmax to vectorize:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement