数据清洗与完善
TechCorp 正在迁移到新的 HR 系统。数据集需要清洗:移除因录入错误导致的低薪异常值,删除新系统不会使用的列,并添加一个计算得到的奖金字段。数据清洗通常占据分析时间的 80%,这些技能至关重要。
Table、Selection 和 DoubleColumn 类已为您导入。
本练习是课程的一部分
Java 中的数据导入
练习说明
- 移除薪资低于 $40,000 的员工。
- 删除
"JobTitle"列。 - 添加
PerformanceBonus列(薪资的 5%)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
public class DataExploration {
public static void main(String[] args) {
Table employees = Table.read().csv("employees.csv");
// Remove employees with salaries below $40,000
Selection lowSalaries = employees.intColumn("Salary").isLessThan(____);
Table cleanedEmployees = employees.____(lowSalaries);
// Remove the JobTitle column
Table streamlined = cleanedEmployees.____("JobTitle");
DoubleColumn performanceBonus = streamlined.intColumn("Salary").asDoubleColumn()
.map(salary -> salary * 0.05);
performanceBonus.setName("PerformanceBonus");
// Add the PerformanceBonus column
Table enhancedEmployees = streamlined.____(performanceBonus);
System.out.println("Total employees after cleaning: " + enhancedEmployees.rowCount());
System.out.println("\nFirst 5 rows of enhanced dataset:");
System.out.println(enhancedEmployees.first(5));
}
}