SBad
SBad

Reputation: 1345

if condition + check isnull() is true

I have a df that looks like this:

     carry_dt    sedol    prime_broker_id  legal_entity   strategy  NewCarryRate  
716   2018-01-02  B1T84Z4    CITI             USSA-AGG      USSA       NaN

I would like to check if there are any NaN values in the column 'NewCarryRate' and replace them

I created an if condition:

 if (allHoldings['NewCarryRate'].iloc[[i]].isnull())==True:
        allHoldings['NewCarryRate'].values[i]= 100

I get an error saying:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I know I can use np.where but I find it easier to use if condition here because I can add more rows under the if condition if I wanted to.

Many Thanks for your help

Upvotes: 2

Views: 8722

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76317

You can just use pd.Series.fillna for this:

allHoldings['NewCarryRate'].fillna(100, inplace=True)

The entire if clause is unnecessary - fillna does this. The reason you're getting this error is that

if (allHoldings['NewCarryRate'].iloc[[i]].isnull())==True:

it can be true for some entries and false for some; the interpreter can't know if to perform the clause or not.

Upvotes: 2

Related Questions