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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值