Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

How to add "Total" cell and the Sum of a column in Pandas? And how do I style them?

$
0
0

I have a pandas dataframe that looks like this:

col_1   col_2a       10b       20c       30d       40

But I want to turn it into this, with a "Total" column that shows the sum of col_2 that skips 1 row. How do I do this in pandas dataframe?

col_1   col_2a       10b       20c       30d       40(empty row)total   100

I tried df.loc["Total"] = df.sum(numeric_only=True), but this only results in the sum of col_2 being shown, and not the "Total" next to it.

Also, how can I style the two cells of "Total" and "col_2 sum" after the fact in openpyxl?

I want to use this, but this assumes a static position of the column, and the position of "Total" and "col_2 sum" changes with each row of the dataframe.

for cell in ws["3:3"]:    cell.fill = PatternFill(start_color='DDEBF7', end_color='DDEBF7', fill_type="solid")    cell.font = Font(name="Segoe UI Light", size=11, bold=True)

Thanks in advance


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>