在大Pandas之后附加到列表

2024-05-20 23:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将time值附加到plotList,只要dup列的值是False。你知道吗

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()

有人能解释一下a)这里发生了什么,b)我能做些什么来避免吗?我不明白问什么是False怎么会模棱两可。你知道吗

非常感谢。你知道吗


Tags: 代码idfalsetruedftimesegmentdiff
2条回答

我只想在dup列上使用否定~进行筛选,因为您正在筛选False。你知道吗

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

我想你可以这样做:

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

相关问题 更多 >