gpt4 book ai didi

python - 函数不适用于 pandas 数据框,出现语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:51 24 4
gpt4 key购买 nike

我正在尝试将此函数应用于 pandas 数据框,以查看出租车上车或下车时间是否落在我使用下面的到达最小、到达最大变量创建的范围内。

如果时间确实落入范围内,我想保留该行。如果它超出范围,我想将其从数据框中删除。

Start.Time、End.Time 等都是日期时间对象,因此时间功能应该可以正常工作。

def time_function(df, row):
gametimestart = df['Start.Time']
gametimeend = df['End.Time']
arrivemin = gametimestart - datetime.timedelta(minutes=120)
arrivemax = gametimeend - datetime.timedelta(minutes = 30)
departmin = gametimeend - datetime.timedelta(minutes = 60)
departmax = gametimeend + datetime.timedelta(minutes = 90)
for not i in ((df['pickup_datetime'] > arrivemin) & (df['pickupdatetime'] < arrivemax) &(df['dropoff_datetime'] > departmin) & (df['dropoffdatetime'] < departmax)):
df = df.drop[df[i.index]]
return


for index, row in yankdf:
time_function(yankdf, row)

不断出现此语法错误:

 File "<ipython-input-25-bda6fb2db429>", line 17
for not i in (((row['pickup_datetime'] > arrivemin) & (row['pickupdatetime'] < arrivemax)) | ((row['dropoff_datetime'] > departmin) & (row['dropoffdatetime'] < departmax)):
^
SyntaxError: invalid syntax

最佳答案

我认为您不需要该功能。只需执行一个基本子集,df_filtered 就应该是您过滤后的数据帧。

gametimestart = df['Start.Time'] 
gametimeend = df['End.Time']
arrivemin = gametimestart - datetime.timedelta(minutes=120)
arrivemax = gametimeend - datetime.timedelta(minutes = 30)
departmin = gametimeend - datetime.timedelta(minutes = 60)
departmax = gametimeend + datetime.timedelta(minutes = 90)
df_filtered = df[(df['pickup_datetime'] > arrivemin) &
(df['pickup_datetime'] < arrivemax) &
(df['dropoff_datetime'] > departmin) &
(df['dropoffdatetime'] < departmax)]

关于python - 函数不适用于 pandas 数据框,出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com