String to Numeric Practice Problem
This data science coding problem helps you practice Data Type Conversion & Validation, string to numeric, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Data Type Conversion & Validation.
- Problem ID: 41
- Problem key: 41-string-to-numeric
- URL: https://datacrack.app/solve/41-string-to-numeric
- Difficulty: easy
- Topic: Data Type Conversion & Validation
- Module: Data Cleaning
Problem Statement
# String to Numeric
### 🎯 Goal
Convert string columns in a DataFrame to numeric types, gracefully handling non-numeric values by coercing them to `NaN`.
### 💻 Task
Implement `convert_to_numeric(data, columns)` that:
1. Converts the input dictionary to a DataFrame
2. Applies `pd.to_numeric` with `errors='coerce'` to each specified column
3. Returns the resulting DataFrame
---
### 📥 Input
- `data`: A dictionary where keys are column names and values are lists of string data
- `columns`: A list of column names to convert to numeric
### 📤 Output
- A pandas DataFrame with specified columns converted to numeric types (`NaN` for unconvertible values)
---
### 🧩 Starter Code
```python
import pandas as pd
import numpy as np
def convert_to_numeric(data, columns):
"""
Convert string columns to numeric types using pd.to_numeric.
Args:
data (dict): Input data as dictionary with string values
columns (list): List of column names to convert
Returns:
pd.DataFrame: DataFrame with specified columns converted to numeric
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Loop through each column and apply pd.to_numeric with errors='coerce'
# TODO: Return the resulting DataFrame
pass
```
---
### 💡 Examples
**Example 1:** Mixed valid and invalid strings
```python
data = {"price": ["10", "20.5", "thirty", "40"], "qty": ["5", "N/A", "15", "20"]}
convert_to_numeric(data, ["price", "qty"])
```
```
price qty
0 10.0 5.0
1 20.5 NaN
2 NaN 15.0
3 40.0 20.0
```
**Example 2:** All valid strings
```python
data = {"value": ["100", "200", "300"]}
convert_to_numeric(data, ["value"])
```
```
value
0 100.0
1 200.0
2 300.0
```
**Example 3:** Placeholder strings become NaN
```python
data = {"score": ["85.5", "-", "90", "N/A", "78.3"]}
convert_to_numeric(data, ["score"])
```
```
score
0 85.5
1 NaN
2 90.0
3 NaN
4 78.3
```Starter Code
import pandas as pd
import numpy as np
def convert_to_numeric(data, columns):
"""
Convert string columns to numeric types using pd.to_numeric.
Args:
data (dict): Input data as dictionary with string values
columns (list): List of column names to convert
Returns:
pd.DataFrame: DataFrame with specified columns converted to numeric
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Loop through each column and apply pd.to_numeric with errors='coerce'
# TODO: Return the resulting DataFrame
pass