0

I have a dataset in this form

Name Industry Volume
Aardec   Food    456 
Aardec   Food    93
Aardec   Food    56
Laven    Ship    45
Laven    Ship    354
Rituor   Parts   963
Rituor   Parts   22
Ritour   Parts   76
Rituor   Parts   86

Here I need to make a new column and insert the sum of volume for each companies according to it's name. So, here is my approach.

volume_count=df.groupby(['name])['volume'].sum()
new_df=pd.Dataframe('volume')

Now I got the output value as

Name   Volume
Aardec  606
Laven   399
Rituor  1127

Finally, I make a new column named as "count" and entered the values:

df['count'][df['ship_to_name']=='Aardec']=606
df['count'][df['ship_to_name']=='Laven']=399
df['count'][df['ship_to_name']=='Rituor']=1127

Finally I got the o/p as:

    Name   Industry Volume  count
    Aardec   Food    456   606
    Aardec   Food    93    606
    Aardec   Food    56    606
    Laven    Ship    45    399
    Laven    Ship    354   399
    Rituor   Parts   963   1127
    Rituor   Parts   22    1127
    Ritour   Parts   76    1127
    Rituor   Parts   86    1127

Although I got the o/p but the problem here is that I am using the hardcoding approch here as I am manually inserting the count values for each name. Is there any better approach to solve this without hardcoding

desertnaut
  • 52,940
  • 19
  • 125
  • 157
Rishavv
  • 301
  • 1
  • 6

0 Answers0