0

I have a DF, Sample below:

Group       $        Type    
1           50       A
1           0        B
1           0        C
2           150      A
2           0        B
2           0        C

What I want to do is populate the $ column with the value associated with the column A, by each group.

Resulting DF will look like the below:

Group       $        Type    
1           50       A
1           50       B
1           50       C
2           150      A
2           150      B
2           150      C

I have tried various np.where functions but can't seem to get the desired output.

Thanks in advance!

Tickets2Moontown
  • 117
  • 1
  • 10
  • if the A type is the only value present in column `$` for each group then just `df['$'].replace(0, np.nan).ffill()` – It_is_Chris Jan 05 '22 at 15:21

1 Answers1

1

Try with groupby with transform max

df['new$'] = df.groupby('Group')['$'].transform('max')
df
Out[371]: 
   Group    $ Type  new$
0      1   50    A    50
1      1    0    B    50
2      1    0    C    50
3      2  150    A   150
4      2    0    B   150
5      2    0    C   150
BENY
  • 317,841
  • 20
  • 164
  • 234