0
$\begingroup$

I have a dataframe shaped like below

size ind weight weighted_hour
1    A    17       40
1    B    20       41
1    C    19       42
2    A    50       30
2    B    20       10
2    C    13       16
3    A    17       19
3    B    21       23
3    C    23       30

and I want to insert some sum of criteria like below

size ind     weight weighted_hour Total_WH
1    A       17       40          57
1    B       20       41          61
1    C       19       42          61
1    Total   56       123         179
2    A       50       30          80
2    B       20       10          30
2    C       13       16          29
2    Total   83       56          139
3    A       17       19          36
3    B       21       23          44
3    C       23       30          53
3    Total   61       72          133

I prefer to use pandas but if someone can suggest more efficient method It's good either. Give me some advice. Thank you!

$\endgroup$
1
$\begingroup$

Assuming you have the above dataframe as df:

df.weight = df.weight.astype(int)
df.weighted_hour = df.weighted_hour.astype(int)
df["Total_WH"] = df["weight"] + df["weighted_hour"]

df1 = df.groupby(['size'], as_index=False).sum().assign(ind='Total')

df = pd.concat([df, df1]).sort_values('size')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.