Home > Blockchain >  python for loop with if statement to divide numbers
python for loop with if statement to divide numbers

Time:02-14

if statement and for loop

I am stuck with the following code, I have a column in which I want to divide by 2 if the number is above 10 and run this for all the rows. I have tried this code but it gives the error of the series is ambiguous:

if df[x] > 10:
   df[x]/2
else:
   df[x]

I suppose that I need a for loop in combination with the if statement. However I could not make it running, anyone has some ideas?

CodePudding user response:

It's not clear exactly what you're asking, but assuming you want to divide the element at x if it is > 10, you can simply do

>>> df = [1,2,3,4,5,6]
>>> df = [1,2,30,40,5,6]
>>> if df[2]>10:
...     df[2]/=2
... 
>>> df
[1, 2, 15.0, 40, 5, 6]

Note the /= instead of your /.

CodePudding user response:

The easiest approach, I think, is to use boolean indexing. For example:

df = pd.DataFrame(           # create dataframe
    20*np.random.rand(6, 4), 
    columns=list("ABCD"))
print(df)                    # print df
df[df>10]/=2                 # divide entries over 10 by 2
print(df)                    # print df

Result:

           A          B          C          D
0   1.245686   1.443671  17.423559  17.617235
1  13.834285  10.482565   2.213459   9.581361
2   0.290626  14.082919   0.224327  11.033058
3   5.113568   5.305690  19.453723   3.260354
4  14.679005   8.761523   2.417432   4.843426
5  15.990754  12.421538   4.872804   5.577625

          A         B         C         D
0  1.245686  1.443671  8.711780  8.808617
1  6.917143  5.241283  2.213459  9.581361
2  0.290626  7.041459  0.224327  5.516529
3  5.113568  5.305690  9.726862  3.260354
4  7.339503  8.761523  2.417432  4.843426
5  7.995377  6.210769  4.872804  5.577625
  • Related