進階過濾與分析
TechCorp 想篩選具高潛力、可培養為領導者的員工:30 歲以下、但已拿到 Mid 或 Senior 等級薪資的年輕員工。你需要先移除離群值,接著把數值薪資轉成類別等級,最後再結合多個過濾條件。這些技巧能處理單純過濾無法回答的真實商業問題。
Table、Selection、StringColumn 類別已為你匯入。
本練習屬於課程
Java 中的資料匯入
練習說明
- 移除離群值(salary < $40K 或 > $250K)。
- 在過濾後計算被移除的列數。
- 將薪資映射為
"Junior"、"Mid"、"Senior"等級。 - 篩選 30 歲以下且薪資等級為
"Mid"/"Senior"的員工。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
public class DataExploration {
public static void main(String[] args) {
// Start with the high earners table from previous exercises
Table employees = Table.read().csv("employees.csv");
// Remove outliers (salary < $40K or > $250K)
Selection outliers = employees.intColumn("Salary").isLessThan(____)
.or(employees.intColumn("Salary").isGreaterThan(____));
Table cleanedData = employees.____(outliers);
// Count rows removed after filtering
System.out.println("Employees after removing outliers: " + (employees.____() - cleanedData.____()));
// Map salaries to Junior, Mid, and Senior grades
StringColumn salaryGroup = cleanedData.intColumn("Salary").map(
salary -> {
if (salary < 100000) return "____";
else if (salary < 200000) return "Mid";
else if (salary >= 200000) return "____";
else return "Error";
},StringColumn::create).setName("SalaryGrade");
cleanedData = cleanedData.addColumns(salaryGroup);
// Filter for Mid/Senior grades under age 30
Selection highPotential = cleanedData.stringColumn("____")
.isEqualTo("Mid")
.or(cleanedData.stringColumn("SalaryGrade").isEqualTo("Senior"))
.and(cleanedData.intColumn("____").isLessThan(30));
Table highPotentialEmployees = cleanedData.where(____);
System.out.println(highPotentialEmployees.rowCount());
System.out.println(highPotentialEmployees.first(5));
}
}