Table filtering
Now that you have the basic product catalog loaded, the startup wants to focus on premium products for their advanced training courses. You need to identify which products are considered "premium" (priced above $800) and create a filtered dataset for the marketing team.
The Table, DoubleColumn, and Selection classes have been imported for you.
This exercise is part of the course
Importing Data in Java
Exercise instructions
- Create a
Selectionfor"Price"greater than800. - Filter the products table using the
Selection. - Print the row count of premium products.
- Print the mean price of premium products.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class ProductCatalog {
public static void main(String[] args) {
try {
Table products = Table.read().csv("products.csv");
// Create a Selection for Price greater than 800
Selection premiumSelection = products.doubleColumn("Price")
.____(____);
// Filter the products table using the Selection
Table premiumProducts = products.____(____);
// Print the row count of premium products
System.out.println("Number of premium products: " + premiumProducts.____());
// Print the mean price of premium products
DoubleColumn premiumPrices = premiumProducts.doubleColumn("Price");
System.out.println("Average price of premium products: $" + premiumPrices.____());
} catch (Exception e) {
System.err.println("Error reading CSV files: " + e.getMessage());
}
}
}