Stefania Axo
Stefania Axo

Reputation: 127

pandas dataset 2 column min and max determine a column

I have a dataframe dt like this

             minV   maxV
2008-01-02    NaN    NaN
2008-01-03    NaN    NaN
2008-01-04  37.33    NaN
2008-01-07    NaN  37.71
2008-01-08  36.21    NaN
2008-01-09    NaN    NaN
2008-01-10    NaN  37.70

on each row I can have either a minV or a maxV.

I can not have on the same day a value NOT NaN for both minV and maxV. (not possible 2008-01-08 36.21 37.71)

I want to create another dataframe where only on days that have either a minV or maxV not NaN:

             val    
2008-01-04   1000.00   
2008-01-07  -1000.00 
2008-01-08   1000.00 
2008-01-10  -1000.00

1000 is a constant

Upvotes: 0

Views: 31

Answers (1)

Chris
Chris

Reputation: 16172

You can use dropna with all to get rid of any rows that are completely null, then use np.where to populate your values conditionally.

import numpy as np  
df = df.dropna(how='all')    
df['val'] = np.where(df['minV'].isnull(), -1000,1000)    
print(df[['val']])

Output

             val
2008-01-04  1000
2008-01-07 -1000
2008-01-08  1000
2008-01-10 -1000

Upvotes: 2

Related Questions