2

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

SBad
  • 1,245
  • 5
  • 23
  • 36
  • You use a double array at `.iloc[[i]]` don't you mean `.iloc[i]`? And you dont have to use the `== True` it's redundant – Ron Nabuurs May 18 '18 at 09:24
  • @Ron Thank you for you answer i did `.iloc[[i]]` to select the row i. is it not correct? also when I do `allHoldings['NewCarryRate'].iloc[i].isnull())` i get an error – SBad May 18 '18 at 09:28
  • I'm not sure about what is correct, I've never used pandas. But what are you trying to accomplish with it? Do you want to check if the whole row is null? – Ron Nabuurs May 18 '18 at 09:31
  • 1
    And have a look at this [answer](https://stackoverflow.com/a/36922103/5686835) – Ron Nabuurs May 18 '18 at 09:34
  • I am looking to check if the column['NewCarryRate'] of row i is NaN – SBad May 18 '18 at 10:48
  • 1
    @SBad actually, according to your description, you're just looking to replace such values by 100; explicitly check if row i is NaN is not your end in itself. – Ami Tavory May 18 '18 at 11:11

1 Answers1

2

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.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185