ValueError when running Python function in data pipeline

Question:

I’m building a data pipeline using Python and I’m running into an issue when trying to execute a certain function. The error message I’m receiving is: ValueError: Could not convert string to float: 'N/A'

Here is the function in question:

def process_data(data):
    for item in data:
        # Do some processing...
        value = float(item[1])
        if value > 0:
            processed_item = process_item(item)
            yield processed_item

I’m calling the function like this:

data = [('A', '1.5'), ('B', '2.7'), ('C', 'N/A'), ('D', '4.1'), ('E', '5.9')]
processed_data = process_data(data)

Code:

def process_data(data):
    for item in data:
        # Do some processing...
        value = float(item[1])
        if value > 0:
            processed_item = process_item(item)
            yield processed_item

data = [('A', '1.5'), ('B', '2.7'), ('C', 'N/A'), ('D', '4.1'), ('E', '5.9')]
processed_data = process_data(data)

Error message:

ValueError: Could not convert string to float: 'N/A'

The expected outcome was to process the items in the data list and yield the processed items if the value of the item was greater than 0.

Asked By: Kingdavid Ochai

||

Answers:

The parameter value of float(parameter) must be a number or a string that can be converted into a floating point number.

The value ‘N/A’ cannot be converted because it is not a number.

You could try:

try:
    value = float(item[1])
except ValueError:
    value = 0

Assuming you want anything that is not a number to become zero, which will then be filtered out by your if value > 0: statement.

Answered By: Stamper