Summing Column Values in DataFrame

Write a Pandas program to calculate the sum of the examination attempts by the students.

Example 1:

Input: DataFrame with 'attempts' column 
Output: Sum of 'attempts'

Example 2:

Input: DataFrame with 'attempts' column 
Output: Sum of 'attempts'

Use the pandas DataFrame sum() function.

import pandas as pd

def sum_attempts(df):
    return df['attempts'].sum()

df1 = pd.DataFrame({'name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'attempts': [1, 2, 3, 4]})
df2 = pd.DataFrame({'name': ['Adam', 'Eve', 'Stark', 'Rogers'], 'attempts': [4, 3, 2, 1]})

print(sum_attempts(df1))
print(sum_attempts(df2))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!