2016-04-19 51 views
0

我試圖在dup列值爲False的任何地方附加time值到plotList在熊貓之後添加if else else語句

的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 

守則=

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

我想這可能工作,但我得到的錯誤ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

任何人都可以解釋)是怎麼回事在這裏,和b)我可以做些什麼來避免?我不明白怎麼問是否False可能不明確。

非常感謝。

+0

是什麼'row'?你可以做一個完整的例子來生成你的數據並應用'pullLine'函數嗎? –

+0

'df ['time'] [〜df ['dup']]'應該工作嗎?或者,您需要使用apply:'df.apply(pullLine,axis = 1)'將按預期與'plotList'一起工作。 – jeremycg

回答

2

我想你可以這樣來做:

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

你傳遞整個DF作爲參數傳遞給你的函數,但把它當作一個行...

視你怎麼想 - 數組或列表:

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'] 
1

我只想篩選的dup柱,使用否定~因爲你過濾虛假。

>>> df[~df.dup].time 
0 2015-12-24 02:03:10 
1 2015-12-24 02:03:12 
4 2015-12-24 02:03:19 
5 2015-12-24 02:03:22 
Name: time, dtype: object 

如果你真的想在一個列表格式:

df[~df.dup].time.tolist() 
['2015-12-24 02:03:10', 
'2015-12-24 02:03:12', 
'2015-12-24 02:03:19', 
'2015-12-24 02:03:22']