Instagram
youtube
Facebook
Twitter

Compute the Median Value of a Column in a Pandas DataFrame

Compute the Median Value of a Column in a Pandas DataFrame

Short Description of the Program:
This program calculates the median of the 'Age' column in a Pandas DataFrame using the .median() function.

Explanation:

  • df['Age'] accesses the Age column.
     
  • .median() returns the middle value when the data is sorted.
     
  • It is a measure of central tendency and is useful when data contains outliers.

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)

median_age = df['Age'].median()

print("Median Age:", median_age)