BasicsPandasEDAMetaAmazonUber
Problem
Implement col_cardinality_and_mode(df, col) returning (num_unique, most_frequent_value) for column col, ignoring NaN values.
Why It Matters: Inspecting feature cardinality (nunique) and mode (value_counts) is essential during exploratory data analysis (EDA) and categorical encoding.
Function Signature
import pandas as pd
def col_cardinality_and_mode(df: pd.DataFrame, col: str):
Sample Input & Output
Example 1: Standard Categorical Column
Input:
| color |
|---|
| red |
| blue |
| red |
| green |
| NaN |
Parameter: 'color' |
Expected Output:
(3, 'red')
Explanation: 3 unique non-null colors (red, blue, green); 'red' appears twice so it is the mode.
Example 2: Single Unique Value
Input:
| flag |
|---|
| 1 |
| 1 |
| 1 |
Parameter: 'flag' |
Expected Output:
(1, 1)
Explanation: Cardinality is 1 and the mode is 1.
Constraints & Edge Cases
- Return
(num_unique, most_frequent_value). - Cast
num_uniqueto Pythonint. - Both
.nunique()and.value_counts()ignoreNaNby default.
Constraints & Evaluation Criteria
- Return `(num_unique, most_frequent_value)`.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
['NYC','LA','NYC',None]
Expected Output / Criteria
(2, 'NYC')