表格篩選
你已經載入了基本的產品目錄,這間新創公司想把重點放在進階訓練課程用的高階產品。你需要找出哪些產品被視為「高階」(價格高於 $800),並為行銷團隊建立一個經過篩選的資料集。
Table、DoubleColumn 和 Selection 類別已為你匯入。
本練習屬於課程
Java 中的資料匯入
練習說明
- 建立一個
Selection,條件為"Price"大於800。 - 使用該
Selection篩選 products 表格。 - 印出高階產品的列數。
- 印出高階產品的平均價格。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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());
}
}
}