DataFrame report
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| product | object |
| quarter_1 | int |
| quarter_2 | int |
| quarter_3 | int |
| quarter_4 | int |
+-------------+--------+
Write a solution to reshape the data so that each row represents sales data for a product in a specific quarter.
The result format is in the following example.
Example 1:
Input: +-------------+-----------+-----------+-----------+-----------+ | product | quarter_1 | quarter_2 | quarter_3 | quarter_4 | +-------------+-----------+-----------+-----------+-----------+ | Umbrella | 417 | 224 | 379 | 611 | | SleepingBag | 800 | 936 | 93 | 875 | +-------------+-----------+-----------+-----------+-----------+ Output: +-------------+-----------+-------+ | product | quarter | sales | +-------------+-----------+-------+ | Umbrella | quarter_1 | 417 | | SleepingBag | quarter_1 | 800 | | Umbrella | quarter_2 | 224 | | SleepingBag | quarter_2 | 936 | | Umbrella | quarter_3 | 379 | | SleepingBag | quarter_3 | 93 | | Umbrella | quarter_4 | 611 | | SleepingBag | quarter_4 | 875 | +-------------+-----------+-------+ Explanation: The DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're changing the shape of some information. The brute force method will try out absolutely every possible way to re-arrange and combine the initial data until we find the right shape. It's like trying every combination lock until one opens.
Here's how the algorithm would work step-by-step:
def melt_data_brute_force(data, id_vars, value_vars):
melted_data = []
# Iterate over each row in the original data
for row in data:
# Iterate through the columns to be melted
for column_name in value_vars:
# Create new row with variable and value
new_row = {}
for id_var in id_vars:
new_row[id_var] = row[id_var]
# Store the column name as the variable.
new_row['variable'] = column_name
# Get the value from the original column.
new_row['value'] = row[column_name]
melted_data.append(new_row)
return melted_dataThe goal is to transform data from a wide format to a long format. We'll selectively pick columns and 'unpivot' them into rows, repeating the identifying information as needed. This organized restructuring gives us a new representation of the data.
Here's how the algorithm would work step-by-step:
def melt_data(data, id_vars, value_vars):
melted_data = []
# Iterate over each row in the original data.
for row in data:
# Iterate over the columns to be melted.
for column_to_melt in value_vars:
# Create a new dictionary for each melted value.
new_row = {}
# Copy the identifier variables into the new row.
for id_variable in id_vars:
new_row[id_variable] = row[id_variable]
# Add the variable name and value to the new row.
new_row['variable'] = column_to_melt
new_row['value'] = row[column_to_melt]
# Append the new row to the melted data list.
melted_data.append(new_row)
return melted_data
def example_usage():
data = [{
'ID': 1, 'A': 10, 'B': 20, 'C': 30
}, {
'ID': 2, 'A': 40, 'B': 50, 'C': 60
}]
id_vars = ['ID']
value_vars = ['A', 'B', 'C']
melted_data = melt_data(data, id_vars, value_vars)
print(melted_data)
if __name__ == '__main__':
example_usage()| Case | How to Handle |
|---|---|
| records is null or empty | Return an empty list immediately as there is no data to reshape. |
| id_vars is null or empty | If no identifier variables are provided, the value variables should be melted, with each row potentially duplicated for each value variable. |
| value_vars is null or empty | If no value variables are provided, return an empty list or the original list of records, depending on problem definition (clarify during interview). |
| id_vars and value_vars have overlapping column names | This should be handled gracefully, likely giving precedence to id_vars or raising an exception, depending on the desired behavior (clarify during interview). |
| A column name in id_vars or value_vars does not exist in the records | Raise an exception or skip the invalid column name, depending on desired behavior (clarify during interview). |
| Records have inconsistent schemas (different sets of columns) | Handle the schema of the first record, and if others don't conform, either skip the row or raise an exception (clarify during interview). |
| Large number of records and/or value_vars, causing memory issues | Ensure solution is memory-efficient and avoids creating large intermediate data structures, potentially using generators if the output is very large. |
| Data types of values in value_vars are mixed (e.g., string, int, null) | The solution should handle various data types without errors and store them appropriately in the 'value' field, likely using a generic type or string conversion. |