開始使用免費開始

轉換文字資料

你注意到雜貨庫存資料庫中的產品名稱格式不一致。有些包含括號(例如「Apple (Organic)」)、多餘空白,還有大小寫混用。這會讓比對與分析變得困難。請將這些名稱標準化:移除括號內的描述、清理前後空白,並轉換為小寫。

本練習屬於課程

使用 Java 進行資料清理

檢視課程

練習說明

  • 將產品 names 透過清理作業進行 map。
  • 從每個名稱中移除括號。
  • 移除每個名稱前後的空白。
  • 將每個名稱轉換為小寫。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

public class GroceryDataTransformation {
    public static void main(String[] args) {
        Table inventory = Table.read().csv("grocery_inventory.csv");

        StringColumn names = inventory.stringColumn("Product_Name");
        StringColumn standardizedNames =
            // Map product names through cleaning operations
            ____.____(
                // Remove parentheses
                t -> t.____("\\(.*\\)", "")
                      // Remove surrounding space
                      .____()
                      // Convert to lowercase
                      .____())
            .setName("Standardized_Names");

        System.out.println("Example name before cleaning: " + names.get(84));
        System.out.println("Example name after cleaning: " + standardizedNames.get(84));
    }
}
編輯並執行程式碼