Groupby On Columns With Overlapping Groups
Continuing from my previous question. This produces a dafatrame with 81 columns and filled with random numbers: import pandas as pd import itertools import numpy as np col = 'A,B,
Solution 1:
This won't be possible with groupby as any given column can only be in one group, not in multiple groups. To solve this case you can modify my first solution to the previous question:
cols = sorted([(x[0],x[1]) for x inset([(x.split('.')[0], x.split('.')[-1]) for x in df.columns])])
for c0,c1 in cols:
for n inrange(2,10):
df[f'{c0}.SUM{n}.{c1}'] = df.filter(regex = f'{c0}\.(1|{n})\.{c1}').sum(axis=1)
(This will work for single-digit numbers (1 through 9) in the column headers as per your example. If there are numbers > 9 you'll have to modify the regex accordingly.)
Post a Comment for "Groupby On Columns With Overlapping Groups"