abhi1610
abhi1610

Reputation: 743

Divide individual grouped items in pandas

Following df:

    appid   tag totalvalue
0   1234    B   50.00
1   1234    BA  10.00
2   2345    B   100.00
3   2345    BA  25.00
4   2345    BCS 15.00

What we want is to group the df with appid and have some analysis based on tag column, is such that if each tag is divided by tag='B' with totalvalue. Just like follows:

   appid    tag total    %tage(B)
0   1234    B   50.00    1    
1   1234    BA  10.00    0.2
2   2345    B   100.00   1
3   2345    BA  25.00    0.4
4   2345    BCS 15.00    0.15

Upvotes: 0

Views: 36

Answers (1)

Corralien
Corralien

Reputation: 120439

You can use groupby:

gmax = df['totalvalue'].where(df['tag'] == 'B').groupby(df['appid']).transform('max')
df['%tage(B)'] = 1 / (gmax / df['totalvalue'])
print(df)

# Output
   appid  tag  totalvalue  %tage(B)
0   1234    B        50.0      1.00
1   1234   BA        10.0      0.20
2   2345    B       100.0      1.00
3   2345   BA        25.0      0.25
4   2345  BCS        15.0      0.15

Upvotes: 2

Related Questions