เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การแปลงข้อมูลตัวเลข

ในฐานะนักวิเคราะห์สินค้าคงคลังของร้านขายของชำ คุณต้องคำนวณมูลค่ารวมของสต็อกสินค้า ข้อมูลที่มีอยู่แสดงจำนวนสินค้าและราคาต่อหน่วย แต่ต้องนำทั้งสองมารวมกันเพื่อทราบมูลค่าสินค้าคงคลัง เนื่องจากจำนวนสินค้าถูกจัดเก็บเป็นจำนวนเต็ม จึงต้องแปลงเป็น double ก่อนนำไปคูณกับราคา จากนั้นคำนวณมูลค่ารวมของแต่ละสินค้าและสร้างคอลัมน์ใหม่เพื่อติดตามการลงทุนในสินค้าคงคลัง

แพ็กเกจ tablesaw ได้นำเข้าให้แล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การทำความสะอาดข้อมูลใน Java

ดูคอร์ส

คำแนะนำการฝึกหัด

  • แสดงชนิดข้อมูลของแต่ละคอลัมน์
  • แปลงคอลัมน์จำนวนเต็ม Stock_Quantity ให้เป็นคอลัมน์ double
  • คูณ quantity ด้วย unitPrice

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

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

        System.out.println("Column datatypes:");
        for (String columnName : inventory.columnNames()) {
            // Print the data type of each column
            System.out.println(columnName + ": " + inventory.____(columnName).____());
        }

        // Convert the integer column to a double column
        DoubleColumn quantity = inventory.____("Stock_Quantity").____();
        DoubleColumn unitPrice = inventory.doubleColumn("Unit_Price");

        // Multiply the quantity by the unit price
        DoubleColumn totalValue = ____.____(unitPrice)
                .setName("Total_Value");
        inventory.addColumns(totalValue);

        System.out.println("\nMultiplying two columns: Stock_Quantity * Unit_Price = Total_Value");
        System.out.println(inventory.selectColumns("Product_Name", "Stock_Quantity", "Unit_Price", "Total_Value")
                .first(4).print());
            }
}
แก้ไขและรันโค้ด