Groupby, map and sum in Pandas resulting in NaN

  • A+
Category:Languages

Data Multiple rows per ID

ID Value1 Value2 1    1     0 1    0     1 1    3     1 

Desired output

For each ID, (SUM(Value1))*(Value2).

In this case it would be 4*0=0 for ID1.

I want the result to be placed back in the original table like below

ID Value1 Value2 Calculated_Value 1    1     0        0 1    0     1        4 1    3     1        4 

I have tried this..it ran but I got NaN in the output table..

df['Calculated_Value'] = df['ID'].map(df.groupby('ID')['Value1'].sum()*['Value2']) 


You can do this with .transform:

df['Calculated_Value'] = df.groupby('ID').Value2. /                             transform(lambda x: x * df['Value1'].sum())  print df  #    ID  Value1  Value2  Calculated_Value # 0   1       1       0                 0 # 1   1       0       1                 4 # 2   1       3       1                 4 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: