Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Summing up rows and columns
#1
result = [[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9, 10, 11, 12],
          [13, 14, 15, 16]]

qq = sum(result[3])
A. I can sum up all the rows using sum(). But how to sum the columns? Beside using the long method result[0][0]+result[1][0]+result[2][0]+result[3][0].

B. sum() will not work if one of the list elements is blank. Any way to resolve this?

Many thanks.

P/S

Found the answers. I should try harder n my search! Ha.

https://www.geeksforgeeks.org/python-col...sted-list/
Reply
#2
You could transpose the rows into columns and then use sum
from itertools import zip_longest

rows = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

print(rows)


def transpose_rows(rows):
    return tuple(zip_longest(*rows, fillvalue=0))


columns = transpose_rows(rows)

print(columns)

print(sum(columns[3]))
Output:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] ((1, 5, 9, 13), (2, 6, 10, 14), (3, 7, 11, 15), (4, 8, 12, 16)) 40
Reply
#3
(Aug-13-2021, 06:19 AM)Yoriz Wrote: You could transpose the rows into columns and then use sum
from itertools import zip_longest

rows = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

print(rows)


def transpose_rows(rows):
    return tuple(zip_longest(*rows, fillvalue=0))


columns = transpose_rows(rows)

print(columns)

print(sum(columns[3]))
Output:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] ((1, 5, 9, 13), (2, 6, 10, 14), (3, 7, 11, 15), (4, 8, 12, 16)) 40
Noted. Good idea!
Reply
#4
arr1 = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
arr2 = [list(col) for col in zip(*arr1)]
print(arr2)
print(sum(arr2[3]))
Output:
[[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] 40
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  making variables in my columns and rows in python kronhamilton 2 269 Oct-31-2021, 10:38 AM
Last Post: snippsat
  rows from sql query need to write to a file as columns sjcsvatt 6 410 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 593 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 696 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 2,389 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 2,368 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  How to split dataframe object rows to columns Mekala 1 896 Nov-12-2020, 04:18 PM
Last Post: michael1789
  Numpy Play with Rows and Columns of a Matix quest 6 1,170 Nov-05-2020, 09:08 PM
Last Post: quest
  Grouping and summing of dataset jef 0 689 Oct-04-2020, 11:03 PM
Last Post: jef
  Why is first argument sometimes rows and sometimes columns? Mark17 1 1,052 Sep-03-2020, 02:12 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020