Search code examples
pythonpandasnumpydata-analysis

Pandas combining same row into new column while preserving the row(not a simple group-by)


I am a pandas beginner and in need of some help.

I have the following pandas dataframe:

ID         Val-A   Val-B

aab12      lower   -30
dbc11      lower   -10
aab12      upper   50
dbc11      upper   20

I want to produce a new dataframe from the previous one and can't think of a way to do it:

ID         Val-A  Val-B  upper-lower
aab12      lower  -30    80
aab12      upper  50     80
dbc11      lower  -10    30
dbc11      upper  20     30   

Any help will be highly appreciated!


Solution

  • I think you need sort_values first with reset_index for nice monotonic unique index and then transform with abs and sum:

    df = df.sort_values('ID').reset_index(drop=True)
    df['upper-lower'] = df['Val-B'].abs().groupby(df['ID']).transform(sum)
    
    print (df)
          ID  val-A  Val-B  upper-lower
    0  aab12  lower    -30           80
    1  aab12  upper     50           80
    2  dbc11  lower    -10           30
    3  dbc11  upper     20           30