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

时间:2021-04-23 14:21:26

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.

我在用python编写一个for循环,它将一个数组px中的第I个元素与另一个数组py中的第I个元素进行比较。如果px中的元素大于或等于py,我想把这个值记为True或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:

最终的dataframe应该如下所示:

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.

我知道我的for循环有一些问题。

2 个解决方案

#1


3  

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:

如果您想要速度,不应该在数组中进行迭代。相反,可以在矢量化操作中使用df['status'] = px >= py进行比较。您的问题不清楚数据是否已经在Dataframe中,所以从头开始:

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())

#2


1  

For one, the i you use as index is not defined. Instead just use x and y that you already get from the for loop.

首先,作为索引使用的i没有定义。而是用你已经从for循环中得到的x和y。

#1


3  

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:

如果您想要速度,不应该在数组中进行迭代。相反,可以在矢量化操作中使用df['status'] = px >= py进行比较。您的问题不清楚数据是否已经在Dataframe中,所以从头开始:

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())

#2


1  

For one, the i you use as index is not defined. Instead just use x and y that you already get from the for loop.

首先,作为索引使用的i没有定义。而是用你已经从for循环中得到的x和y。