BasicsPandasColumnsMetaAmazonUber
Problem
Implement select_and_rename(df) that selects only the userId and totalSpend columns from df and renames them to snake_case: user_id and total_spend.
Why It Matters: Normalizing camelCase API/JSON columns into clean snake_case warehouse schemas is a staple ETL step.
Function Signature
import pandas as pd
def select_and_rename(df: pd.DataFrame) -> pd.DataFrame:
Sample Input & Output
Example 1: Drop Extra Columns & Rename
Input:
| userId | name | totalSpend |
|---|---|---|
| 10 | Ava | 42.5 |
| 11 | Ben | 99.0 |
Expected Output:
| user_id | total_spend |
|---|---|
| 10.0 | 42.5 |
| 11.0 | 99.0 |
Explanation: name is dropped; userId -> user_id and totalSpend -> total_spend.
Example 2: Single-Row Input
Input:
| extra | totalSpend | userId |
|---|---|---|
| x | 0.0 | 1 |
Expected Output:
| user_id | total_spend |
|---|---|
| 1.0 | 0.0 |
Explanation: Reorders columns into ['user_id', 'total_spend'] regardless of input column order.
Constraints & Edge Cases
- Return a 2-column DataFrame with columns
['user_id', 'total_spend']. - Drop all other columns not in
['userId', 'totalSpend']. - Preserve row order and column order
['user_id', 'total_spend'].
Constraints & Evaluation Criteria
- Return a 2-column DataFrame with columns `['user_id', 'total_spend']`.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
3-col DataFrame
Expected Output / Criteria
cols ['user_id', 'total_spend']