Context: I'm trying to get the sum of the groups created using a groupby using a list of patterns that are present on the dataframe columns.
For example, let's say we have this dataframe:
df = pd.DataFrame({'123_Pattern1_a':[0,1,2],'X_Y_Pattern2_X':[3,4,5],'Z_D_Pattern2_Y':[4,5,7],'312_Pattern1_Z':[8,2,4]})
I now would like to create a group by using the "Pattern" and get the sum of values for those columns for each row
If we have a list like this:
pattern = ['Pattern1','Pattern2']
With the dataframe above, the output should be another dataframe as such:
df_final = pd.DataFrame({'Pattern1':[8,3,6],'Pattern2':[7,9,12]})
Basically, "concatenating" all the columns that have a specific pattern on the given column name and get the sum of these values by row
I was trying something like this:
pattern = ['Pattern1','Pattern2','Pattern3',...]
grouped = pd.DataFrame(data_media.groupby(data_media.columns.str.extract(pattern, expand=False), axis=1))
But it doesn't work since extract is a regex and I'm using a list with the patterns. How could I create a regex that would work for this problem? Or is there another way to do this?