0

Is there any way to make the below code more efficient.

for i in range(0, len(df)):
    current_row = df.iloc[i]    
    if i > 0:
        previous_row =df.iloc[i-1]
    else:
        previous_row = current_row 
    if (current_row['A'] != 1):
        if ((current_row['C'] < 55) and (current_row['D'] >= -1)):
            df.loc[i,'F'] = previous_row['F'] + 1
        else: 
            df.loc[i,'F'] = previous_row['F']

For example if the dataframe is like below:

df = pd.DataFrame({'A':[1,1,1, 0, 0, 0, 1, 0, 0], 'C':[1,1,1, 0, 0, 0, 1, 1, 1], 'D':[1,1,1, 0, 0, 0, 1, 1, 1],
'F':[1,1,1, 0, 0, 0, 1, 1, 1]})

My output should look like this

>>> df
   A  C  D  F
0  1  1  1  1
1  1  1  1  1
2  1  1  1  1
3  0  0  0  2
4  0  0  0  3
5  0  0  0  4
6  1  1  1  1
7  0  1  1  2
8  0  1  1  3

So basically, if the conditions are met, I want the value of 'F' to be modified as the previous row of 'F' +1.

I also tried below code but It dint work.

df['prev'] = df.F.shift()
def shift(row):
    row['F'] = np.where((row['A'] != 1) & ((row['C']<55) & (row['D']>=-1)), row['prev'] + 1, row['prev'])
    return row['F']

df['F'] = df.apply(shift, axis=1)
Shanoo
  • 1,185
  • 1
  • 11
  • 38
  • Welcome to DS SE! In general, specific programming problems such as this one are better suited for Stackoverflow. Please refer to the DS on-topic section for questions: https://datascience.stackexchange.com/help/on-topic –  Oct 17 '19 at 02:52
  • The problem here is that the calculation of the current row is dependent on the calculation of the previous row. So it need to be done in a loop, see: https://stackoverflow.com/questions/34855859/is-there-a-way-in-pandas-to-use-previous-row-value-in-dataframe-apply-when-previ – Shaido Oct 18 '19 at 08:22

1 Answers1

0

Create a new column F_previous using df.F.shift(1) so you will get 1-shifted values as a new column. Now write a function using that column and other columns to return new F value and use apply method to get new values for F column.

Uday
  • 281
  • 4
  • 9