始める無料で始める

数値データの変換

あなたは食料品店の在庫アナリストとして、在庫の総額を計算する必要があります。データには商品の数量と単価が含まれていますが、在庫の価値を把握するためにこれらを組み合わせる必要があります。数量は整数として格納されているため、価格と掛け合わせる前に double 型に変換する必要があります。各商品の合計価値を計算し、在庫投資額を管理する新しい列を作成しましょう。

tablesaw パッケージはあらかじめインポートされています。

この演習はコースの一部です

Java によるデータクリーニング

コースを見る

演習の手順

  • 各列のデータ型を表示します。
  • 整数列 Stock_Quantity を double 列に変換します。
  • quantityunitPrice を掛け合わせます。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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());
            }
}
コードを編集して実行