Chapter 30 Short pandas Exercises
30.0.0.1 DataFrame Creation:
Create a Pandas DataFrame with two columns, “Name” and “Age”, with three rows of data: names of your choice and corresponding ages.
import pandas as pd
data = {"Name": ["alice", "betty", "adam"],
"Age": [22, 24, 21]}
df = pd.DataFrame(data)
df## Name Age
## 0 alice 22
## 1 betty 24
## 2 adam 21
30.0.0.2 Selecting Data:
Given a DataFrame df with columns “Product”, “Price”, and “Quantity”, how would you select only the “Price” column?
data = {"Product": ["x", "y", "z"],
"Price": [10, 15, 13],
"Quantity": [20, 50, 100]}
df = pd.DataFrame(data)
## selecting a column and return data frame
df[["Price"]]## Price
## 0 10
## 1 15
## 2 13
30.0.0.3 Filtering Rows:
Using the DataFrame df from question 2, write a command to filter for rows where “Price” is greater than $10.
## Product Price Quantity
## 1 y 15 50
## 2 z 13 100
## Product Price Quantity
## 1 y 15 50
## 2 z 13 100
30.0.0.4 Adding a New Column:
In a DataFrame with columns “Length” and “Width”, how would you create a new column called “Area” that multiplies “Length” and “Width” for each row?
data = {"width": [2, 3, 4], "height": [10, 12, 10]}
df = pd.DataFrame(data)
## area column
df['area'] = df['width']*df['height']
df.head()## width height area
## 0 2 10 20
## 1 3 12 36
## 2 4 10 40
30.0.0.5 GroupBy Operation:
Suppose you have a DataFrame with columns “Category” and “Sales”. How would you group the data by “Category” and calculate the total “Sales” for each category?
categories = ["luxury", "medium", "premium", "medium", "premium","luxury", "medium"]
sales = [5, 3, 10, 30, 2, 5, 10]
data = {"category": categories, "sales": sales}
df = pd.DataFrame(data)
## group-by
df.groupby('category')['sales'].sum()## category
## luxury 10
## medium 43
## premium 12
## Name: sales, dtype: int64
Handling Missing Data: If a DataFrame df has some missing values, what command would you use to fill these with a default value, such as 0? Sorting Data: In a DataFrame df with columns “Date” and “Sales”, how would you sort the rows by “Sales” in descending order?