我有df数据。我需要能够根据星期几返回某些数据行。
如果是星期一,我需要打印包含过去3天数据的行。 如果是星期三或星期五,我需要打印包含过去两天数据的行。
合并两个df以创建一个:
<sequence name="seTestChangeNamespace" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
(...)
<enrich>
<source type="body"/>
<target type="property" property="INPUT_MESSAGE"/>
</enrich>
<enrich>
<source type="inline">
<myns:Envelope xmlns:myns="http://schemas.xmlsoap.org/soap/envelope/">
<myns:Body/>
</myns:Envelope>
</source>
<target type="envelope"/>
</enrich>
<enrich>
<source type="property" property="INPUT_MESSAGE"/>
<target type="body"/>
</enrich>
(...)
</sequence>
根据今天的日期获取正确的日期:
df_new = pd.concat([outcomes_df, specialists_df], ignore_index=True)
df_new['Published Date'] = pd.to_datetime(df_new['Published Date'])
按日期范围过滤
N=0
if datetime.today().weekday() == 0:
N = 3
elif datetime.today().weekday() == 2 or datetime.today().weekday() == 4:
N = 2
else:
pass
mydate = datetime.now() - timedelta(days=N)
print(mydate)
df_new = df_new[(df_new['Published Date'] >= mydate) & (df_new['Published Date'] <= datetime.today())]
得出正确的日期
print(mydate)
导致错误:
空数据框 列:[col1,col2,col3,发布日期] 索引:[]
答案 0 :(得分:0)
这里不需要if,因为这也需要循环,在这里我正在使用np.select
import numpy as np
s=df_new['Published Date'].dt.weekday
d=np.select([s==0,s.isin([2,4])],[3,2],0)
df_new['mydate']=(pd.to_datetime('today')-pd.to_timedelta(d,unit='D'))
df_new = df_new[(df_new['Published Date'] >= df_new['mydate']) & (df_new['Published Date'] <= datetime.today())]