Practical Data Manipulation Techniques

Practical Data Manipulation Techniques

Welcome to Practical Data Manipulation Techniques! In this unit, we’ll combine and build upon everything you've learned about data transformation in Ruby. You’ll work through techniques for filtering, projecting, and aggregating data, using methods like map, select, sum, and reduce. By the end, you’ll know how to harness these methods to analyze and summarize data effectively.

Let’s dive in!

Setting Up Our Dataset

Throughout this unit, we’ll work with a structured dataset to apply and combine the techniques you’ve learned. Here’s an array of hashes representing individuals with different attributes:

Ruby
data = [
  { 'name' => 'Alice', 'age' => 25, 'profession' => 'Engineer', 'salary' => 70000 },
  { 'name' => 'Bob', 'age' => 30, 'profession' => 'Doctor', 'salary' => 120000 },
  { 'name' => 'Carol', 'age' => 35, 'profession' => 'Artist', 'salary' => 50000 },
  { 'name' => 'David', 'age' => 40, 'profession' => 'Engineer', 'salary' => 90000 }
]

This dataset will be the foundation as we explore data manipulation techniques.

Selecting Specific Fields (Data Projection)

Data projection is used to select specific fields from each entry in a dataset. Let’s say we only want to see each person's name and profession:

Ruby
projected_data = data.map do |entry|
  entry.select { |key| ['name', 'profession'].include?(key) }
end

puts projected_data
# Output:
# [
#   {"name"=>"Alice", "profession"=>"Engineer"},
#   {"name"=>"Bob", "profession"=>"Doctor"},
#   {"name"=>"Carol", "profession"=>"Artist"},
#   {"name"=>"David", "profession"=>"Engineer"}
# ]

In this example:

  1. map iterates through each person in the dataset.
  2. select extracts only the name and profession fields.

The result is an array of hashes containing only the projected fields.

Filtering Data Based on Conditions

Filtering allows you to keep only the data that matches specific conditions. Let’s select only the individuals who are 30 years or older:

Ruby
filtered_data = data.select { |entry| entry['age'] >= 30 }

puts filtered_data
# Output:
# [
#   {"name"=>"Bob", "age"=>30, "profession"=>"Doctor", "salary"=>120000},
#   {"name"=>"Carol", "age"=>35, "profession"=>"Artist", "salary"=>50000},
#   {"name"=>"David", "age"=>40, "profession"=>"Engineer", "salary"=>90000}
# ]

Here:

  1. select filters entries where the age is 30 or above.
  2. The result contains only entries matching this age criterion.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal