Compare Elements In Two Arrays And Return True When One Value Is Greater Than The Other Using Python
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 tha
Solution 1:
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())
Solution 2:
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.
Post a Comment for "Compare Elements In Two Arrays And Return True When One Value Is Greater Than The Other Using Python"