鉴于下面的数据集,我想过滤所有具有属于过去7个工作日的日期的记录。
record_id,date,site,sick,funny,happy
CDEC1947-6,9/2/2018,2,1,1,1
IJKC1953-4,9/29/2018,2,1,1,1
FGHC1724-9,10/25/2018,2,3,1,1
FGHC2929-1,10/31/2018,4,1,1,1
CDEC1912-0,11/1/2018,1,1,1,1
IJKC1726-4,11/2/2018,1,3,1,1
IJKC1728-0,10/26/2018,2,3,1,1
ABCC1730-6,11/2/2018,2,3,1,1
ABCC1731-4,11/2/2018,2,3,1,1
CDEC1733-0,10/22/2018,1,3,1,1
CDEC1735-5,11/2/2018,2,3,1,1
IJKC1914-6,10/27/2018,2,6,1,1
ABCC1916-1,10/23/2018,2,6,1,1
IJKC1918-7,11/2/2018,2,1,1,1
CDEC1920-3,10/24/2018,1,6,1,1
IJKC1943-5,11/2/2018,2,4,1,1
ABCC1945-0,11/2/2018,1,4,1,1
ABCC1949-2,10/25/2018,2,4,1,1
CDEC1951-8,11/2/2018,2,5,1,1
CDEC2924-2,11/3/2018,4,1,1,1
CDEC2927-5,11/3/2018,1,1,1,1
ABCC2925-9,11/4/2018,4,1,1,1
IJKC1941-9,11/4/2018,2,4,1,1
ABCC2922-6,11/5/2018,1,1,1,1
我尝试了许多技巧,但没有成功。 以下是其中之一:
df['data_recrutamento'] = pd.to_datetime(df['data_recrutamento'])
m1 = (df['sick'] == 1) | (df['funny'] == 1) | (df['happy'] == 1)
m2 = df['date'] >= pd.Timestamp('today') - pd.DateOffset(days=7)
m3 = ~df['date'].dt.weekday.isin([5, 6])
dates_last7_weekdays = df.loc[m1 & m2 & m3, 'site'].value_counts()
dates_last7_weekdays
dates_last7_weekdays = df.loc[m1 & m2 & m3, 'site'].value_counts()
dates_last7_weekdays
其他尝试示例:
import pandas as pd
import numpy as np
from plotly.offline import init_notebook_mode, iplot
from plotly.graph_objs import *
import plotly.graph_objs as go
import datetime
from datetime import date
from datetime import timedelta
today = date.today()
from IPython.core.interactiveshell import InteractiveShell
%matplotlib inline
df=pd.read_csv("dataset.csv", encoding="utf-8",low_memory=False)
df["date"]=pd.to_datetime(df["date"])
df["site"]=df["site"].astype("category") # Convert to category
df['sick']=df['sick'].astype('category')
df["funny"]=df["funny"].astype("category")
df["happy"]=df["happy"].astype("category")
df = df.sort_values(by='date', ascending='True')
df.head()
record_id date site sick funny happy
0 CDEC1947-6 2018-09-02 2 1 1 1
1 IJKC1953-4 2018-09-29 2 1 1 1
9 CDEC1733-0 2018-10-22 1 3 1 1
12 ABCC1916-1 2018-10-23 2 6 1 1
14 CDEC1920-3 2018-10-24 1 6 1 1
2 FGHC1724-9 2018-10-25 2 3 1 1
17 ABCC1949-2 2018-10-25 2 4 1 1
6 IJKC1728-0 2018-10-26 2 3 1 1
11 IJKC1914-6 2018-10-27 2 6 1 1
3 FGHC2929-1 2018-10-31 4 1 1 1
4 CDEC1912-0 2018-11-01 1 1 1 1
7 ABCC1730-6 2018-11-02 2 3 1 1
10 CDEC1735-5 2018-11-02 2 3 1 1
5 IJKC1726-4 2018-11-02 1 3 1 1
13 IJKC1918-7 2018-11-02 2 1 1 1
15 IJKC1943-5 2018-11-02 2 4 1 1
16 ABCC1945-0 2018-11-02 1 4 1 1
18 CDEC1951-8 2018-11-02 2 5 1 1
8 ABCC1731-4 2018-11-02 2 3 1 1
19 CDEC2924-2 2018-11-03 4 1 1 1
20 CDEC2927-5 2018-11-03 1 1 1 1
22 IJKC1941-9 2018-11-04 2 4 1 1
21 ABCC2925-9 2018-11-04 4 1 1 1
23 ABCC2922-6 2018-11-05 1 1 1 1
days_diff = []
for i in df.loc[:, 'date']:
days_diff.append(((datetime.datetime.today() - i).days))
final=df[(pd.Series(days_diff) <= 7) & ((df.loc[:, 'sick'] == 1)|(df.loc[:, 'funny'] == 1)|(df.loc[:, 'happy'] == 1) )]
C:\Users\H\Miniconda3\lib\site-packages\ipykernel_launcher.py:10: UserWarning:
Boolean Series key will be reindexed to match DataFrame index.
len(final)
21
final
record_id date site sick funny happy
9 CDEC1733-0 2018-10-22 1 3 1 1
12 ABCC1916-1 2018-10-23 2 6 1 1
14 CDEC1920-3 2018-10-24 1 6 1 1
17 ABCC1949-2 2018-10-25 2 4 1 1
11 IJKC1914-6 2018-10-27 2 6 1 1
10 CDEC1735-5 2018-11-02 2 3 1 1
13 IJKC1918-7 2018-11-02 2 1 1 1
15 IJKC1943-5 2018-11-02 2 4 1 1
16 ABCC1945-0 2018-11-02 1 4 1 1
18 CDEC1951-8 2018-11-02 2 5 1 1
19 CDEC2924-2 2018-11-03 4 1 1 1
20 CDEC2927-5 2018-11-03 1 1 1 1
22 IJKC1941-9 2018-11-04 2 4 1 1
21 ABCC2925-9 2018-11-04 4 1 1 1
23 ABCC2922-6 2018-11-05 1 1 1 1
但是我希望的结果最多只能是行中的7个不同日期,而不能超过该日期,因为我只想使用今天的日期作为参考过滤过去7个工作日。因此,根据数据集,我的目标输出不应包括周末2018-11-04、2018-11-03和这些日期2018-10-22、2018-10-23、2018-10-的日期24、2018-10-25和2018-10-27不应该包括在内,因为它们不属于过去7个工作日。因此,我的最终输出应仅为:
record_id date site sick funny happy
10 CDEC1735-5 2018-11-02 2 3 1 1
13 IJKC1918-7 2018-11-02 2 1 1 1
15 IJKC1943-5 2018-11-02 2 4 1 1
16 ABCC1945-0 2018-11-02 1 4 1 1
18 CDEC1951-8 2018-11-02 2 5 1 1
23 ABCC2922-6 2018-11-05 1 1 1 1
因为这些日期属于与2018年11月6日至2018年11月29日的最后7个工作日相对应的日期间隔(今天是我撰写此2018-11-06时的参考日期,但明天应该是2018年11月-07)。
答案 0 :(得分:1)
一种直接的方法是减去并找到日差,并将其用于子集。我们使用datetime.datetime.today()
来获取今天的日期时间。然后,我们使用该日期时间从您的df.loc[:, 'dates']
列中减去每个条目。为了确保不会随着时间的推移而浪费时间,我们在末尾使用(...).days
。然后,我们使用比较小于或等于运算来创建布尔序列,指示哪些条目小于或等于7天。最后,我们使用该布尔系列过滤数据框
import datetime
days_diff = []
for i in df.loc[:, 'date']:
days_diff.append(((datetime.datetime.today() - i).days))
#subset your data frame
df[pd.Series(days_diff) <= 7]
#or to include the other conditions as well,
df[(pd.Series(days_diff) <= 7) & ((df.loc[:, 'sick'] == 1)|(df.loc[:, 'funny'] == 1)|(df.loc[:, 'happy'] == 1) )]
注意:首先将date
列转换为正确的日期时间