2

I'm converting values to float from multiple columns in a dataset concurrently in a loop, where I try to skip bad rows with a ValueError exception

My data has been loaded with a structure that enables me to loop over it as such

A_floated = []
B_floated = []

for i in range(len(data)):

    A = data[i]["column_A"]
    B = data[i]["column_B"]

    try:
        A = float(A)
        A_floated.append(A)

        B = float(B)
        B_floated.append(B)

    except(ValueError):
        continue

I want the exception to encompass both A and B, so that if any of them contains a bad row, all of them will be skipped for this i, so that len(A_floated) == len(B_floated). Right now, I can only figure out how to skip bad rows on an individual-column basis, but then I end up with floated columns of different lengths.

How can this be done?

2 Answers 2

3

If I understand you correctly, you want to gather 'good' (floated) rows in data structures A_floated and B_floated respectively during iteration through data. If so, try putting the former data structures outside the loop, and check whether values can be converted into floats before putting them into lists. Otherwise, value A may be convertible while value B not, which would result in different lengths of A_floated and B_floated. To sum it up, try the following code:

A_floated = []
B_floated = []

for i in range(len(data)):

    A = data[i]["column_A"]
    B = data[i]["column_B"]

    try:
        # "floatability" check.
        A = float(A)
        B = float(B)

        # If both values can be converted into a float,
        # put them into the respective lists.
        A_floated.append(A)
        B_floated.append(B)

    except(ValueError):
        continue
Sign up to request clarification or add additional context in comments.

2 Comments

Oops, I forgot to put the empty lists outside of the loop while I wrote the example! Sorry for the blunder.
Also, I didn't know that both of the float checks needed to be called first before appending, and that it would be different from a try block with a potential error somewhere inside. Thanks.
1

Use can use the else clause which runs if no exception occurs. try and convert, don't do any appending there and, if no errors occur, you append in the else clause:

try:
    A = float(A)
    B = float(B)
except ValueError:
    continue
else:
    A_floated.append(A)
    B_floated.append(B)

I removed the parentheses around the ValueError since they aren't necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.