函数来查找日期是否存在,如果存在则打印数据帧的该行



我正在尝试创建一个函数,该函数接收一个日期,并在日期列中扫描DF以查找该日期,日期列采用日期时间格式。一旦它找到日期,我希望它从数据帧中返回匹配的订单行:我已经设法这样开始了,但我无法获得我想要的回报:

例如DF

date     orders
5351660 2020-08-03  1011
5351719 2020-08-04  1012
5812943 2020-08-05  1013
5814499 2020-08-06  2565
5350549 2020-08-07  5555
5350484 2020-08-08  2546
5813992 2020-08-09  8945
5351067 2020-08-10  6965
5350968 2020-08-11  1236```

```def weekoforders(date):
x = df1['date'].eq(date).any() else 'no'
return df1[df1['orders'] == x]
weekoforders('2020-08-07')

您可以尝试:

def weekoforders(date):
cond=df['date'].isin([pd.to_datetime(date)])
if cond.any():
return df.loc[cond,'orders']
return 'No'
weekoforders('2021-08-07')

相关内容

最新更新