列筛选
作为一名杂货库存分析师,您需要清洗库存数据,以找出有变质风险的易腐商品。原始库存数据通常包含无关类别和不准确的库存数量。为清洗这些数据,您将筛选出 "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));
}
}