Advanced filtering and analysis
TechCorp wants to identify high-potential employees for leadership development: young employees (under 30) already earning Mid or Senior-level salaries. This requires removing outliers, creating categorical salary grades from numeric data, and then combining multiple filter conditions. These techniques handle real business questions that simple filters can't answer.
The Table, Selection, and StringColumn classes have been imported for you.
This exercise is part of the course
Importing Data in Java
Exercise instructions
- Remove outliers (salary < $40K or > $250K).
- Count rows removed after filtering.
- Map salaries to
"Junior","Mid", and"Senior"grades. - Filter for
"Mid"/"Senior"grades under age 30.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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));
}
}