0

I'm trying to create an array based on values from another data frame in Python. I want it to fill the array as such.

If x > or = 3 in the dataframe then it inputs a 0 in the array. 
If x < 3 in the dataframe then it inputs a 1 in the array.  
If x = 0 in the dataframe then it inputs a 0 in the array.

Below is the code I have so far but the result is coming out as just [0]

array = np.array([])

for x in df["disc"]:
    for y in array:    
        if x >= 3:
            y=0
        elif x < 3:
            y=1
        else:
            y=0

Any help would be much appreciated thank you.

1
  • You are not doing anything to array. Did you mean to add the y values to array? I don't think you need a nested loop here. Commented Aug 7, 2020 at 11:43

3 Answers 3

2

When working with numpy arrays, it is more efficient if you can avoid using explicit loops in Python at all. (The actual looping takes place inside compiled C code.)

disc = df["disc"]

# make an array containing 0 where disc >= 3, elsewhere 1
array = np.where(disc >= 3, 0, 1)

# now set it equal to 0 in any places where disc == 0
array[disc == 0] = 0

It could also be done in a single statement (other than the initial assignment of disc) using:

array = np.where((disc >= 3) | (disc == 0), 0, 1)

Here the | does an element-by-element "or" test on the boolean arrays. (It has higher precedence than comparison operators, so the parentheses around the comparisons are needed.)

Sign up to request clarification or add additional context in comments.

5 Comments

I believe it would be slightly more efficient to do this in a single statement rather than passing through the array again. That is, something like array = np.where(disc >= 3 or disc == 0, 0, 1). This would avoid checking 0 equality for those values >= 3, thanks to the short circuiting of or, right?
@Zaxutic That's a possibility, thanks. You would have to use | instead of or though.
@Zaxutic I don't think the short-circuit evaluation is going to be available, because the operands of the | are still going to have to be boolean arrays that will need to be evaluated, but it might still be a bit quicker so I'll add it as an option. (or would attempt to apply a truth value to the whole array.)
@Zaxutic note also that when using |, extra parentheses will be needed because, unlike or, the | has higher precedence than comparison operators
@Zaxutic Final comment - it might use more memory because both temporary boolean arrays will need to exist at the same time.
0

This is a simple problem. There are many ways to solve this, I think the easiest way is to use a list. You can use a list and append the values according to the conditions.

array = []

for x in df["disc"]:
   if x >= 3:
       array.append(0)
   elif x < 3:
       array.append(1)
   else:
       array.append(0)

Comments

0

Your code doesn't seem to be doing anything to the array, as you are trying to modify the variable y, rather than the array itself. y doesn't reference the array, it just holds the values found. The second loop also doesn't do anything due to the array being empty - it's looping through 0 elements. What you need rather than another for loop is to simply append to the array.

With a list, you would use the .append() method to add an element, however as you appear to be using numpy, you'd want to use the append(arr, values) function it provides, like so:

array = np.array([])

for x in df["disc"]:  
    if x >= 3:
        array = np.append(array, 0)
    elif x < 3:
        array = np.append(array, 1)
    else:
        array = np.append(array, 0)

I'll also note that these conditions can be simplified to combine the two branches which append a 0. Namely, if x < 3 and x is not 0, then add a 1, otherwise add a 0. Thus, the code can be rewriten as follows.

array = np.array([])

for x in df["disc"]:  
    if x < 3 and x != 0:
        array = np.append(array, 1)
    else:
        array = np.append(array, 0)

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.