Jordy M.
Jordy M.

Reputation: 23

Python - For loop

I have tried a couple of solutions available here on stackoverflow but still incapable of solving the problem. Because I'm just starting with programming it might be a simple solution.

The situation: Currently working on a Pandas dataframe called 'dataset' which contains the column zscore. I would like to determine the cells that are above 3.00 in order set the value of zscore to 3.00. On the other side I would like to have the values in zscore below the -3.00 changed towards -3.00.

The code:

maxzscore = 3.00
minzscore = -3.00

print ('Set the max zscore:', maxzscore)
print ('Set the min zscore:', minzscore)

for value in dataset.zscore:
    # identify zscore above maxzscore
    if value > maxzscore:
        (dataset['zscore'].replace(3.00))
    # identify zscore below minzscore
    elif (dataset['zscore'] < minzscore):
        (dataset['zscore'].replace(-3.00))
    # do nothing
    else:
        pass

   dataset.to_excel('dataset.xls')

The problem: The code loops through the data for an endless time which is not very efficient. Therefore I'm wondering how to shorten the processing time and improve the code.

Help is much appreciated.

Upvotes: 2

Views: 80

Answers (1)

Steven G
Steven G

Reputation: 17152

no need for loop.. just use the clip() method:

   dataset['zscore'] = dataset['zscore'].clip(-3.0, 3.0)

Upvotes: 6

Related Questions