Instagram
youtube
Facebook
Twitter

Compute the Sum of a Column in a Pandas DataFrame

     A Compute the Sum of a Column in a Pandas DataFrame

     Short Description of the Program:
     This program calculates the total sum of values in the 'Age' column using Pandas' .sum() method.

      Explanation:

  • df['Age'] selects the column named 'Age'.
     
  • .sum() adds up all the values in that column.
     
  • Useful for analyzing total counts or aggregate values in data.
     

Program:

import pandas as pd

data = {

    'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eva', 'Frank'],

    'Age': [25, 30, 22, 28, 26, 35],

    'Gender': ['Female', 'Male', 'Male', 'Female', 'Female', 'Male'],

    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Seattle']
}
df = pd.DataFrame(data)

sum_age = df['Age'].sum()

print("Total Age:", sum_age)