Home > front end >  how to put a condition in a loop that checks all cells one by one?
how to put a condition in a loop that checks all cells one by one?

Time:07-25

I have a list of columns of my dataframe named "column". I want to calculate the difference between these columns and df['DOB'] I mean if it contins 0 I want to return 0 and if it is not 0 then calculate the difference.this is the loop for that:

    if df[df[col]>0]:
        df['diff_age' col]=df[col]-df['DOB']
    else:
         df['diff_age' col]=0

but I got this error: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

dataset view needed for condition. can you just share!

CodePudding user response:

IIUC, you can vectorize your operation:

column = ['col1', 'col2', 'col3']
df[column] = df[column].where(df[column] > 0).add(df['DOB'], axis=0).fillna(0, downcast='int')
print(df)

# Output
   col1  col2  col3  DOB
0     0     4     4    3
1     0     5     6    3
2     0     0     7    3
3     0     0     0    0
4     5     5     0    3
5     0     8     0    4
6     2     2     0    0
7     0     3     4    1
8     0     0     0    3
9     4     0     0    3

Minimal Reproducible Example:

>>> df
   col1  col2  col3  DOB
0    -2     1     1    3
1    -4     2     3    3
2    -1    -3     4    3
3    -5    -2    -5    0
4     2     2    -1    3
5    -3     4     0    4
6     2     2    -4    0
7    -5     2     3    1
8     0    -3    -5    3
9     1     0    -2    3
  • Related