Do something in nth row with enumerate

python for loop every nth
python list take every nth element
every nth element of list
python for loop every nth element
python remove every nth element from list
write a function to make a new list made up of every nth element in the original list
get every nth element of list
iterate over every second element in list python

I have two columns which I want to compare every nth row. If it comes across the nth row it will compare them and put the result of the if statement in a new column.

When I tried the enumerate function it always ends up in the true part of the if statement. Somehow this piece of the code is always thrue:

if (count % 3)== 0:

for count, factors in enumerate(df.index):
    if (count % 3)== 0: #every 3th row
        df['Signal']=np.where(df['Wind Ch']>=df['Rain Ch'],'1', '-1')
    else:
        df['Signal']=0

In column 'Signal' I am expecting a '1' or '-1' every 3rd row and '0' on all the other rows. However I am getting '1' or '-1' on each row

Now I am getting:

            Date  Wind CH Rain CH  Signal
    0   5/10/2005  -1.85%  -3.79%       1
    1   5/11/2005   1.51%  -1.66%       1
    2   5/12/2005   0.37%   0.88%      -1
    3   5/13/2005  -0.81%   3.83%      -1
    4   5/14/2005  -0.28%   4.05%      -1
    5   5/15/2005   3.93%   1.79%       1
    6   5/16/2005   6.23%   0.94%       1
    7   5/17/2005  -0.08%   4.43%      -1
    8   5/18/2005  -2.69%   4.02%      -1
    9   5/19/2005   6.40%   1.33%       1
    10  5/20/2005  -3.41%   2.38%      -1
    11  5/21/2005   3.27%   5.46%      -1
    12  5/22/2005  -4.40%  -4.15%      -1
    13  5/23/2005   3.27%   4.48%      -1

But I want to get:

              Date  Wind CH Rain CH  Signal
     0   5/10/2005  -1.85%  -3.79%     0.0
     1   5/11/2005   1.51%  -1.66%     0.0
     2   5/12/2005   0.37%   0.88%    -1.0
     3   5/13/2005  -0.81%   3.83%     0.0
     4   5/14/2005  -0.28%   4.05%     0.0
     5   5/15/2005   3.93%   1.79%     1.0
     6   5/16/2005   6.23%   0.94%     0.0
     7   5/17/2005  -0.08%   4.43%     0.0
     8   5/18/2005  -2.69%   4.02%    -1.0
     9   5/19/2005   6.40%   1.33%     0.0
     10  5/20/2005  -3.41%   2.38%     0.0
     11  5/21/2005   3.27%   5.46%    -1.0
     12  5/22/2005  -4.40%  -4.15%     0.0
     13  5/23/2005   3.27%   4.48%     0.0

What am I missing here?


You can go about it like this, using np.vectorize to avoid loops:

import numpy as np

def calcSignal(x, y, i):
    return 0 if (i + 1) % 3 != 0 else 1 if x >= y else -1

func = np.vectorize(calcSignal)

df['Signal'] = func(df['Wind CH'], df['Rain CH'], df.index)

df

         Date  Wind CH Rain CH  Signal
0   5/10/2005  -1.85%  -3.79%       0
1   5/11/2005   1.51%  -1.66%       0
2   5/12/2005   0.37%   0.88%      -1
3   5/13/2005  -0.81%   3.83%       0
4   5/14/2005  -0.28%   4.05%       0
5   5/15/2005   3.93%   1.79%       1
6   5/16/2005   6.23%   0.94%       0
7   5/17/2005  -0.08%   4.43%       0
8   5/18/2005  -2.69%   4.02%      -1
9   5/19/2005   6.40%   1.33%       0
10  5/20/2005  -3.41%   2.38%       0
11  5/21/2005   3.27%   5.46%      -1
12  5/22/2005  -4.40%  -4.15%       0
13  5/23/2005   3.27%   4.48%       0

Iterate over every nth element in string in loop - python, If you want to do something every nth step, and something else for other cases, you could use enumerate to get the index, and use modulus: MOD takes a number and divisor, and returns the remainder after division, which makes it useful for formulas that need to do something every nth time. In this case, the number is created with the COLUMN function, which return the column number of cell B8, the number 2, minus 1, which is supplied as an "offset".


In general you don't want to loop over pandas objects. This case is no exception.

In [12]: df = pd.DataFrame({'x': [1,2,3], 'y': [10, 20, 30]})                                                                                                                                                                                                                                          

In [13]: df                                                                                                                                                                                                                                                                                            
Out[13]: 
   x   y
0  1  10
1  2  20
2  3  30

In [14]: df.loc[df.index % 2 == 0, 'x'] = 5                                                                                                                                                                                                                                                            

In [15]: df                                                                                                                                                                                                                                                                                            
Out[15]: 
   x   y
0  5  10
1  2  20
2  5  30

The Logic of Infinity, is an enumeration of all sequences Si of natural numbers, then define the nth term In the second row there are m vertices, in the third row there are m2 vertices, from the diagonal is in [0,1), but is not in the original list, i.e. it will be irrational. Select the data range that you want to return every 5 rows, and click Kutools > Select > Select Interval Rows & Columns, see screenshot: 2 . In the popped out Select Interval Rows & Columns dialog box, select Rows option from Select section, and then enter the interval number and row number you want to select into the Interval of and Rows text boxes separately, see screenshot:


there is no need to use enumerate function as i see it.Also your logic is faulty. you are rewriting complete column in every iteration of loop instead of ith row of column. you could simply do this

 for count  in range(len(df.index)):
     if (count % 3)== 0: #every 3th row
         df['Signal'].iloc[count]=np.where(df['Wind Ch'].iloc[count]>=df['Rain Ch'].iloc[count],'1', '-1')
     else:
         df['Signal'].iloc[0]=0

Walk Through Combinatorics, A: An Introduction To Enumeration And , 2n = 13 In other words, the sum of every third element of the nth row of the (50) If we expand the expression (x1 )6, + x2 + x3 + x4 what will be the largest  Get/Extract Every Nth Value In Excel In this post we will see how to extract every nth value/row in excel. We will try to understand how to extract every nth value row in excel with couple of steps mentioned below.


CSS: even and odd rules, CC 2n 3 In other words, the sum of every third element of the nth row of If we expand the expression (a1 + c2 + za + ra)", what will be the largest coefficient that​  One way to do this would be(If I want to iterate over nth element): sample = "This is a string" n = 3 # I want to iterate over every third element i = 1 for x in sample: if i % n == 0: # do something with x else: # do something else with x i += 1 Is thery any "pythonic" way to do this?


Group By: split-apply-combine, tr:nth-child(even) {background: #CCC} tr:nth-child(odd) {background: #FFF} For example, for a long list you could do this: be sure that the background of the columns is visible, you should not set a background on the rows. Row is given the first cell in the same column, and returns 1. This result is multiplied by n, which is 3 in this example to get 3. As the formula is copied down the column, the value returned by ROW increments by 1, which is what creates the "nth pattern".


Extract matrix rows from list - General, These will split the DataFrame on its index (rows). created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Compute last of group values. nth(). Take nth value, or a subset if n is a list. First, we have defined the row number from where we want to insert the row. Then we have defined the range to insert the data. And, then we have defined the number to insert the row after how many rows. And then till which row, rows will be inserted. To run the code press the key F5 on your keyboard. Rows will get inserted till the 50 th row in