hselbie hselbie - 2 months ago 10
Python Question

Appending to List after Pandas if else statement

I'm trying to append the

time
value to
plotList
wherever the
dup
column value is
False
.

The DF =

lat time trip_id diff shifted Segment dup
-7.12040 2015-12-24 02:03:10 18060.0 0.00003 0.00000 1 False
-7.12043 2015-12-24 02:03:12 18060.0 0.00000 0.00003 2 False
-7.12043 2015-12-24 02:03:14 18060.0 0.00003 0.00003 2 True
-7.12046 2015-12-24 02:03:16 18060.0 0.00003 0.00003 2 True
-7.12049 2015-12-24 02:03:19 18060.0 0.00003 0.00000 3 False
-7.12052 2015-12-24 02:03:22 18060.0 0.00000 -0.00473 4 False


The Code =

plotList=[]
def pullLine(row):
if row['dup'] == False:
plotList.append(row['time'])
pullLine(df)


I had thought this might work but I get the error that
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()


Can anyone explain a) what is going on here, and b) what I can do to avoid? I don't understand how asking if something is
False
can be ambiguous.

Many thanks.

Answer

i guess you can do it this way:

plotList = df.loc[df['dup'] == False, 'time'].values

you're passing the whole DF as a parameter to your function, but are treating it as one row...

depending on what do you want to get - array or list:

In [167]: df.loc[df['dup'] == False, 'time'].values
Out[167]:
array(['2015-12-24 02:03:10', '2015-12-24 02:03:12', '2015-12-24 02:03:19',
       '2015-12-24 02:03:22'], dtype=object)

In [168]: df.loc[df['dup'] == False, 'time'].tolist()
Out[168]:
['2015-12-24 02:03:10',
 '2015-12-24 02:03:12',
 '2015-12-24 02:03:19',
 '2015-12-24 02:03:22']