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': ['', '', '', '', '', '',],
})
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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.