Instagram
youtube
Facebook
Twitter

Group a Pandas DataFrame by a Specific Column

Group a Pandas DataFrame by a Specific Column

Short Description of the Program:
This program groups the DataFrame by the 'Gender' column and calculates the mean age for each gender group.

Explanation:

  • groupby('Gender') creates groups based on unique values in the 'Gender' column.
     
  • ['Age'].mean() computes the average age for each group.
     
  • You can also apply other functions like sum(), count(), max(), etc.

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)

grouped = df.groupby('Gender')['Age'].mean()

print(grouped)