python取出数组大于某值,比较两个数组中的元素,并使用python当一个值大于另一个时返回True...

I'm trying to write a for loop in python that compares each ith element in one array px to the ith element in another array py. If the element in px is greater than or equal to that of py than I want to note that value as True or 1.

Here's some code.

import pandas as pd

import random

px = np.random.normal(loc=0, scale=1, size=1000)

py = np.random.normal(loc=0, scale=1, size=1000)

for x, y in zip(px, py):

print("{}% {}".format(x, y))

if px[i] >= py[i]:

px['status'] = True

if px[i] < py[i]:

px['status'] = False

The final dataframe should look something like this:

px py status

-2.24239571e-01 -1.83834445e+00 False

1.20102447e+00 5.01755172e-03 False

8.82060986e-02 -2.55639665e-02 True

I know I have some problems with my for loop.

解决方案

You should not be iterating through arrays if you want speed. Instead, the comparison can be done in a vectorized operation using df['status'] = px >= py. It's not clear from your question if the data is already in a Dataframe, so from scratch:

import numpy as np

import pandas as pd

px = np.random.normal(loc=0, scale=1, size=1000)

py = np.random.normal(loc=0, scale=1, size=1000)

df = pd.DataFrame({'px': px, 'py': py, 'status': px >= py})

print(df.head())