0

My data is as below

Sec issues Gaps
AAA 22 5
BBB 33 2
CCC 44 1

Desired result

Sec issues Gaps
AAA 22 5
BBB 33 2
CCC 44 1
TOTAL 99 8

I try below code

df.append(df.sum(numeric_only=True), ignore_index=True)

but with this I miss sector name and simply get totals at end, I want sector name and totals both

4
  • 2
    Can you elaborate a bit more on your use case for this? It's strange to me that you explicitly want to mix summary data in your DataFrame itself. Commented Sep 27, 2023 at 15:58
  • Try df.loc[len(df)]=['Total', *df.iloc[:,1:].sum()] (related answer) Commented Sep 27, 2023 at 16:38
  • Please include your pd.__version__ in the body of your question. If you use df.append, which is deprecated since version 1.4.0, then answers will have to fit your version. Alternatively, update and use Good alternative to Pandas .append() method, now that it is being deprecated? Commented Sep 28, 2023 at 13:42
  • Does this answer your question? Adding sum row to specific columns in dataframe. I was going to suggest exactly that. Set as index the 'Sec' column, then create new row with df.sum(), finally reset the index (with drop=False) Commented Sep 28, 2023 at 13:52

3 Answers 3

1

Build a new dataframe with additional row (retaining the initial columns):

(pd.DataFrame([*df.values, ['Total', *df.sum(numeric_only=True).values]], 
              columns=df.columns))

     Sec  issues  Gaps
0   AAA       22     5
1   BBB       33     2
2   CCC       44     1
3  Total      99     8
Sign up to request clarification or add additional context in comments.

1 Comment

Cool, upvoted. By the way, this actually works only when the non numeric column is not in between the numeric ones, doesn't it?
0

This solution worked -- df.loc["Total"] = df.sum()

1 Comment

Not quite. The point is to avoid applying the sum to the column of dtype object. You have use OP exact input and show your exact output.
0

About your attempt:

  • What is your pd.__version__? df.append is deprecated since version 1.4.0. Our answers will not necessarily fit your version. Here is mine: 2.0.3
  • The next issue is .sum(numeric_only=True) not having the correct size for insertion, because a non numeric column will be skipped, not written in, as None for example (wish it was).

Alternatively:

Temporarily hiding non numeric columns into the index looks like the simplest alternative and does not suffer from deprecation: (based on Adding sum row to specific columns in dataframe)

df.set_index(  keys='Sec', inplace=True) # Send object column to index
df.loc['TOTAL'] = df.sum()               # Add sum row
df.reset_index(drop=False, inplace=True) # Restore index

Output:

     Sec  issues  Gaps
0    AAA      22     5
1    BBB      33     2
2    CCC      44     1
3  TOTAL      99     8

A note:

  • This method avoids the risk of mixing up sums that might arise with other methods involving reconstructing another dataframe.
  • Slightly outside the scope of your question, this can be adapted to the case where df were to have several non numeric columns scattered in between numeric columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.