การกรองตาราง
หลังจากโหลดแคตาล็อกสินค้าพื้นฐานแล้ว สตาร์ตอัปต้องการเจาะจงไปที่สินค้าพรีเมียมสำหรับคอร์สฝึกอบรมขั้นสูง คุณต้องระบุว่าสินค้าใดถือเป็น "พรีเมียม" (ราคาสูงกว่า $800) และสร้างชุดข้อมูลที่กรองแล้วสำหรับทีมการตลาด
คลาส Table, DoubleColumn และ Selection ถูกนำเข้าให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การนำเข้าข้อมูลใน Java
คำแนะนำการฝึกหัด
- สร้าง
Selectionสำหรับคอลัมน์"Price"ที่มีค่ามากกว่า800 - กรองตารางสินค้าโดยใช้
Selectionที่สร้างขึ้น - แสดงจำนวนแถวของสินค้าพรีเมียม
- แสดงราคาเฉลี่ยของสินค้าพรีเมียม
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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());
}
}
}