開始使用免費開始

欄位篩選

身為雜貨庫存分析師,你需要清理庫存資料,找出有腐敗風險的易腐品。原始庫存資料常包含無關的品類與不精準的庫存量。為了清理這些資料,你會篩選「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));
    }
}
編輯並執行程式碼