Student Activity

Student Activity Guide:

1) Introduction to Pandas DataFrames and Series

  • Create a Pandas DataFrame from a dictionary.

  • Extract and manipulate data using Series.

Example Code:

import pandas as pd

# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
print(df)

# Extracting a column as a Series
ages = df['Age']
print("Ages:", ages)

Student Exercise: Create a DataFrame with student names, marks, and subjects. Extract a specific column as a Series.

2) Data Manipulation and Transformation

  • Add, modify, and delete columns in a DataFrame.

  • Perform data filtering using conditions.

Example Code:

# Adding a new column
df['Score'] = [90, 85, 88]
print(df)

# Filtering data
filtered_df = df[df['Age'] > 28]
print("Filtered DataFrame:", filtered_df)

Student Exercise: Modify an existing column and filter data based on multiple conditions.

3) Handling Missing Data

  • Identify missing values in a DataFrame.

  • Fill or drop missing values appropriately.

Example Code:

# Introducing missing values
df.loc[1, 'Score'] = None
print(df.isnull())  # Check for missing values

# Filling missing values
df.fillna(0, inplace=True)
print(df)

Student Exercise: Load a dataset with missing values and handle them using fillna() and dropna().

4) Merging and Grouping Data

  • Merge two DataFrames using common keys.

  • Group data and calculate aggregate statistics.

Example Code:

# Merging DataFrames
df2 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Salary': [50000, 60000]})
merged_df = pd.merge(df, df2, on='Name', how='left')
print(merged_df)

# Grouping data
grouped_df = df.groupby('City')['Age'].mean()
print("Grouped Data:", grouped_df)

Student Exercise: Merge two datasets containing student scores and details, and group by subjects to calculate average scores.

Last updated