0

I have a data-frame:

df1 = pd.DataFrame({

'YE': [342233.58, 254571.80, 2980.53, 0, 4469.02, 5951.07],
'Q1': [346233.58, -24571.80, 2970.53, 0, 4469.02, 5951.07],
'Q2': [346233.58, -24571.80, 2970.53, 0, 4469.02, 5951.07],
'Q3': [346233.58, -24571.80, 2970.53, 0, 4469.02, 5951.07],
'Q4': [346233.58, -24571.80, 2970.53, 0, 4469.02, 5951.07],
'INFO': ['', '', '', '', '', '',],

})

enter image description here

I would like to return a string 'not ok' in the column INFO if the sum of 'YE' + 'Q1' + 'Q2' + 'Q3' + 'Q4' equals 0.

Else, the string should be: 'ok'.

How do I do that?

toerag
  • 49
  • 3

1 Answers1

1

Use numpy.where with select all columns without INFO, sum and compare by 0:

df1['INFO'] = np.where(df1.drop('INFO', axis=1).sum(axis=1).eq(0), 'not ok','ok')

Or select columns by list:

cols = ['YE', 'Q1', 'Q2', 'Q3', 'Q4']
df1['INFO'] = np.where(df1[cols].sum(axis=1).eq(0), 'not ok','ok')

print (df1)
          YE         Q1         Q2         Q3         Q4    INFO
0  342233.58  346233.58  346233.58  346233.58  346233.58      ok
1  254571.80  -24571.80  -24571.80  -24571.80  -24571.80      ok
2    2980.53    2970.53    2970.53    2970.53    2970.53      ok
3       0.00       0.00       0.00       0.00       0.00  not ok
4    4469.02    4469.02    4469.02    4469.02    4469.02      ok
5    5951.07    5951.07    5951.07    5951.07    5951.07      ok
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252