Open In App

Python | Cumulative Columns summation of Records

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Sometimes, while working with records, we can have a problem in which we need to sum all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using sum() + list comprehension + zip() 
This task can be performed using combination of above functions. In this, we cumulate the like index elements, i.e columns using zip(), and then iterate through them using list comprehension and perform summation using sum().

Python3




# Python3 code to demonstrate working of
# Cumulative Columns summation of Records
# using list comprehension + sum() + zip()
 
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Columns summation of Records
# using list comprehension + sum() + zip()
res =  [sum(ele) for ele in zip(*test_list)]
 
# printing result
print("The Cumulative column sum is : " + str(res))


Output

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column sum is : [8, 15, 17]

The time complexity of the given Python code is O(n), where n is the number of elements in the input list. 
The auxiliary space complexity of the code is O(m), where m is the number of columns in the input list. 

Method #2 : Using zip() + map() + sum() 
This method is similar to the above method. In this, the task performed by list comprehension is performed by map(), which extends the summation of columns to zipped elements.

Python3




# Python3 code to demonstrate working of
# Cumulative Columns summation of Records
# using zip() + map() + sum()
 
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Columns summation of Records
# using zip() + map() + sum()
res = list(map(sum, zip(*test_list)))
 
# printing result
print("The Cumulative column sum is : " + str(res))


Output

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column sum is : [8, 15, 17]

The time complexity of this code is O(n), where n is the number of tuples in the input list. 

The auxiliary space complexity of this code is O(m), where m is the length of each tuple in the input list. 

Method 3 – using a for loop:

 In this we method iterates through each column of the input list using the outer loop and calculates the sum of each column using the inner loop. The sum is then appended to the res list.

Python3




test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Columns summation of Records using for loop
res = []
for i in range(len(test_list[0])):
    col_sum = 0
    for j in range(len(test_list)):
        col_sum += test_list[j][i]
    res.append(col_sum)
 
# printing result
print("The Cumulative column sum is : " + str(res))


Output

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column sum is : [8, 15, 17]

The time complexity of this program is O(n^2), where n is the number of rows or columns in the input list test_list. 
The auxiliary space used by this program is O(n), where n is the number of columns in the input list test_list. 

Method #4: Using NumPy

NumPy is a Python library used for scientific computing, which includes a powerful N-dimensional array object. We can use the sum() function of NumPy to get the cumulative column sum of the records in the list. 

Step-by-step approach:

  • Import numpy as np.
  • Define the input list test_list containing the records whose cumulative column sum is to be calculated.
  • Print the original list using the print() function and the string concatenation operator.
  • Convert the input list test_list into a NumPy array using the np.array() function.
  • Use the np.sum() function to get the cumulative column sum of the NumPy array. The axis parameter is set to 0 to calculate the sum column-wise.
  • Convert the resultant NumPy array into a list using the tolist() method and store it in the res variable.
  • Print the final result using the print() function and the string concatenation operator.

Below is the implementation of the above approach:

Python3




import numpy as np
 
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Columns summation of Records using NumPy
arr = np.array(test_list)
res = np.sum(arr, axis=0).tolist()
 
# printing result
print("The Cumulative column sum is : " + str(res))


OUTPUT:

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column sum is : [8, 15, 17]

Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the input list.
Auxiliary space: O(n*m), where n is the number of rows and m is the number of columns in the input list, due to the creation of the NumPy array.

Method 6 :  using the pandas library.

  1. The pandas.DataFrame() function is used to convert the list of tuples to a pandas DataFrame object.
  2. The DataFrame.sum() method is used to calculate the sum of each column of the DataFrame.
  3. The tolist() method is used to convert the resulting pandas Series object to a list.

Python3




# import pandas library
import pandas as pd
 
# initialize list
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
 
# convert list of tuples to pandas DataFrame
df = pd.DataFrame(test_list)
 
# calculate cumulative column sum
res = df.sum().tolist()
 
# printing original list and result
print("The original list : " + str(test_list))
print("The Cumulative column sum is : " + str(res))


OUTPUT:

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column sum is : [8, 15, 17]

Time complexity: O(nm), where n is the number of tuples in the list and m is the number of elements in each tuple. 

Auxiliary space complexity: O(nm), where n is the number of tuples in the list and m is the number of elements in each tuple. 

Method 7: using the reduce() function:

  1. Initialize the test_list.
  2. Print the original test_list.
  3. Define the lambda function for adding two tuples.
  4. Use reduce() method to apply the lambda function cumulatively to the elements of the test_list. The result of each step is passed as the first parameter to the next step.
  5. The final result is stored in res.
  6. Print the final result, which is the cumulative sum of the columns in the test_list.

Python3




from functools import reduce
 
test_list = [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Cumulative Columns summation of Records
# using reduce() method
res = reduce(lambda x, y: [x[i] + y[i]
                           for i in range(len(test_list[0]))], test_list)
 
# printing result
print("The Cumulative column sum is : " + str(res))
 
# This code is contributed by Jyothi pinjala.


Output

The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column sum is : [8, 15, 17]

Time Complexity: The time complexity of this implementation is O(mn), where m is the length of the input list and n is the length of the tuples in the list. The reduce() function iterates over all m tuples and performs n additions for each tuple.

Space Complexity: The space complexity of this implementation is O(n), where n is the length of the tuples in the input list. The reduce() function creates a single tuple to store the cumulative column sums.



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads