欄位篩選
身為雜貨庫存分析師,你需要清理庫存資料,找出有腐敗風險的易腐品。原始庫存資料常包含無關的品類與不精準的庫存量。為了清理這些資料,你會篩選「Fruits & Vegetables」這個品類,並找出高庫存(超過 50 單位)的項目。這樣可以排除非易腐品與低庫存商品,留下可能供過於求的水果與蔬菜清單。透過清理並聚焦資料,你能快速辨識哪些易腐品需要立即處理。
本練習屬於課程
使用 Java 進行資料清理
練習說明
- 檢查整數欄
Stock_Quantity是否大於 50。 - 檢查字串欄
Category是否等於Fruits & Vegetables。 - 以
highQuantity篩選inventory。 - 為
fruitsAndVegetables新增一個篩選條件。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
public class GroceryDataFiltering {
public static void main(String[] args) {
Table inventory = Table.read().csv("grocery_inventory.csv");
// Check if the integer column is greater than 50
Selection highQuantity = inventory.____("Stock_Quantity").____(50);
// Check if the string column is equal to Fruits & Vegetables
Selection fruitsAndVegetables = inventory.____("Category").____("Fruits & Vegetables");
// Filter inventory by highQuantity
Table highQuantityTable = inventory.____(highQuantity
// Add a filter for fruitsAndVegetables
.____(fruitsAndVegetables))
.sortDescendingOn("Stock_Quantity");
System.out.println("High quantity fruits and vegetables:");
System.out.println(highQuantityTable.selectColumns("Product_Name", "Stock_Quantity").first(10));
}
}