You've learned this already. ✅
Click here to view the next lesson.
Chapter 6: Data Manipulation with Pandas
6.5 Practical Exercises Chapter 6: Data Manipulation with Pandas
Exercise 1: Creating DataFrames
- Create a DataFrame from a dictionary containing columns for
Name
,Age
, andOccupation
. - Add a new row to this DataFrame.
Solution:
import pandas as pd
# 1. Create a DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [28, 34, 45],
'Occupation': ['Engineer', 'Doctor', 'Artist']
})
# 2. Add a new row
new_row = {'Name': 'David', 'Age': 22, 'Occupation': 'Student'}
df = df.append(new_row, ignore_index=True)
print(df)
Exercise 2: Missing Data Handling
- Create a DataFrame with some missing values.
- Fill the missing values with the mean of the respective column.
Solution:
# 1. Create a DataFrame with missing values
df_missing = pd.DataFrame({
'A': [1, None, 3],
'B': [None, 5, 6]
})
# 2. Fill missing values with mean
df_missing.fillna(df_missing.mean(), inplace=True)
print(df_missing)
Exercise 3: Data Wrangling
- Create a DataFrame from a dictionary containing
Product
,Price
, andQuantity
. - Calculate the total price for each product (Price * Quantity) and store it in a new column.
Solution:
# 1. Create a DataFrame
df_products = pd.DataFrame({
'Product': ['Apple', 'Banana', 'Cherry'],
'Price': [1.2, 0.5, 2.0],
'Quantity': [5, 10, 3]
})
# 2. Calculate total price
df_products['Total_Price'] = df_products['Price'] * df_products['Quantity']
print(df_products)
6.5 Practical Exercises Chapter 6: Data Manipulation with Pandas
Exercise 1: Creating DataFrames
- Create a DataFrame from a dictionary containing columns for
Name
,Age
, andOccupation
. - Add a new row to this DataFrame.
Solution:
import pandas as pd
# 1. Create a DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [28, 34, 45],
'Occupation': ['Engineer', 'Doctor', 'Artist']
})
# 2. Add a new row
new_row = {'Name': 'David', 'Age': 22, 'Occupation': 'Student'}
df = df.append(new_row, ignore_index=True)
print(df)
Exercise 2: Missing Data Handling
- Create a DataFrame with some missing values.
- Fill the missing values with the mean of the respective column.
Solution:
# 1. Create a DataFrame with missing values
df_missing = pd.DataFrame({
'A': [1, None, 3],
'B': [None, 5, 6]
})
# 2. Fill missing values with mean
df_missing.fillna(df_missing.mean(), inplace=True)
print(df_missing)
Exercise 3: Data Wrangling
- Create a DataFrame from a dictionary containing
Product
,Price
, andQuantity
. - Calculate the total price for each product (Price * Quantity) and store it in a new column.
Solution:
# 1. Create a DataFrame
df_products = pd.DataFrame({
'Product': ['Apple', 'Banana', 'Cherry'],
'Price': [1.2, 0.5, 2.0],
'Quantity': [5, 10, 3]
})
# 2. Calculate total price
df_products['Total_Price'] = df_products['Price'] * df_products['Quantity']
print(df_products)
6.5 Practical Exercises Chapter 6: Data Manipulation with Pandas
Exercise 1: Creating DataFrames
- Create a DataFrame from a dictionary containing columns for
Name
,Age
, andOccupation
. - Add a new row to this DataFrame.
Solution:
import pandas as pd
# 1. Create a DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [28, 34, 45],
'Occupation': ['Engineer', 'Doctor', 'Artist']
})
# 2. Add a new row
new_row = {'Name': 'David', 'Age': 22, 'Occupation': 'Student'}
df = df.append(new_row, ignore_index=True)
print(df)
Exercise 2: Missing Data Handling
- Create a DataFrame with some missing values.
- Fill the missing values with the mean of the respective column.
Solution:
# 1. Create a DataFrame with missing values
df_missing = pd.DataFrame({
'A': [1, None, 3],
'B': [None, 5, 6]
})
# 2. Fill missing values with mean
df_missing.fillna(df_missing.mean(), inplace=True)
print(df_missing)
Exercise 3: Data Wrangling
- Create a DataFrame from a dictionary containing
Product
,Price
, andQuantity
. - Calculate the total price for each product (Price * Quantity) and store it in a new column.
Solution:
# 1. Create a DataFrame
df_products = pd.DataFrame({
'Product': ['Apple', 'Banana', 'Cherry'],
'Price': [1.2, 0.5, 2.0],
'Quantity': [5, 10, 3]
})
# 2. Calculate total price
df_products['Total_Price'] = df_products['Price'] * df_products['Quantity']
print(df_products)
6.5 Practical Exercises Chapter 6: Data Manipulation with Pandas
Exercise 1: Creating DataFrames
- Create a DataFrame from a dictionary containing columns for
Name
,Age
, andOccupation
. - Add a new row to this DataFrame.
Solution:
import pandas as pd
# 1. Create a DataFrame
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [28, 34, 45],
'Occupation': ['Engineer', 'Doctor', 'Artist']
})
# 2. Add a new row
new_row = {'Name': 'David', 'Age': 22, 'Occupation': 'Student'}
df = df.append(new_row, ignore_index=True)
print(df)
Exercise 2: Missing Data Handling
- Create a DataFrame with some missing values.
- Fill the missing values with the mean of the respective column.
Solution:
# 1. Create a DataFrame with missing values
df_missing = pd.DataFrame({
'A': [1, None, 3],
'B': [None, 5, 6]
})
# 2. Fill missing values with mean
df_missing.fillna(df_missing.mean(), inplace=True)
print(df_missing)
Exercise 3: Data Wrangling
- Create a DataFrame from a dictionary containing
Product
,Price
, andQuantity
. - Calculate the total price for each product (Price * Quantity) and store it in a new column.
Solution:
# 1. Create a DataFrame
df_products = pd.DataFrame({
'Product': ['Apple', 'Banana', 'Cherry'],
'Price': [1.2, 0.5, 2.0],
'Quantity': [5, 10, 3]
})
# 2. Calculate total price
df_products['Total_Price'] = df_products['Price'] * df_products['Quantity']
print(df_products)