0

I have a data-frame of the format

data

where I apply a conditional operator on a series :

list1=[]
if  (fi['datedelta'] <10): list1.append(fi['TC'])

I get the value error

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

I found out a few resources addressing this After which , I tried :

if(np.where(fi['datedelta'] <= 10 & fi['datedelta'] > 0)):list1.append(fi['TC']) 

but I am getting the same error.

Devarshi Goswami
  • 1,035
  • 4
  • 11
  • 26
  • The use of the ":" here is wrong, what do you want to achieve? You can probably get there by using `.loc[...]`. Can you give the a bit more context, and post the sample data not as picture but as text that can be copied? – divingTobi Feb 06 '20 at 10:35

1 Answers1

0

Assuming you are trying to loop through the Dataframe, try this:

list1=[]
for i in range(fi.shape[0]):
    if (fi['datedelta'][i] <10):
        list1.append(fi['TC'][i])

More efficient method -

list1 = list(fi.loc[fi.datedelta<10, 'TC'])
SubhashR
  • 141
  • 7
  • Thanks a bunch! :) could you let me know why ```np.where``` was not working – Devarshi Goswami Feb 06 '20 at 10:57
  • 1
    fi['datedelta'] <= 10 will return a pandas series containing True and False Boolean values and you cannot apply '&' operator on two pandas series objects Try printing just 'fi['datedelta'] <= 10' you will understand better – SubhashR Feb 06 '20 at 11:04