Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconFundamentos del Análisis de Datos con Python
Fundamentos del Análisis de Datos con Python

Chapter 6: Data Manipulation with Pandas

6.5 Practical Exercises Chapter 6: Data Manipulation with Pandas

Exercise 1: Creating DataFrames

  1. Create a DataFrame from a dictionary containing columns for NameAge, and Occupation.
  2. 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

  1. Create a DataFrame with some missing values.
  2. 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

  1. Create a DataFrame from a dictionary containing ProductPrice, and Quantity.
  2. 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

  1. Create a DataFrame from a dictionary containing columns for NameAge, and Occupation.
  2. 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

  1. Create a DataFrame with some missing values.
  2. 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

  1. Create a DataFrame from a dictionary containing ProductPrice, and Quantity.
  2. 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

  1. Create a DataFrame from a dictionary containing columns for NameAge, and Occupation.
  2. 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

  1. Create a DataFrame with some missing values.
  2. 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

  1. Create a DataFrame from a dictionary containing ProductPrice, and Quantity.
  2. 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

  1. Create a DataFrame from a dictionary containing columns for NameAge, and Occupation.
  2. 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

  1. Create a DataFrame with some missing values.
  2. 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

  1. Create a DataFrame from a dictionary containing ProductPrice, and Quantity.
  2. 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)