Instagram
youtube
Facebook
Twitter

Compute the Variance of a Column in a Pandas DataFrame

Compute the Variance of a Column in a Pandas DataFrame

Short Description of the Program:
This program computes the variance of the 'Age' column using the .var() method from the Pandas library.

Explanation:

  • df['Age'] accesses the Age column.
     
  • .var() calculates the variance, which tells us how much the ages deviate from the mean value on average.
     
  • Higher variance means more spread in the 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)

var_age = df['Age'].var()

print("Variance of Age:", var_age)