我有一个以下示例数据框,并希望将具有特定周的最后一个工作日的数据框子集化为单独的数据框。我已经尝试了很多方法但是不能在工作日做。
df =
Date Open High Low Close Adj Close Volume
0 2007-06-01 0.33979 0.33979 0.33979 0.33979 0.33979 1591888
1 2007-06-02 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2 2007-06-04 0.33526 0.33526 0.33526 0.33526 0.33526 3538
3 2007-06-05 0.32113 0.32113 0.32113 0.32113 0.32113 3550
4 2007-06-06 0.34713 0.34713 0.34713 0.34713 0.34713 670
5 2007-06-07 0.34713 0.34713 0.34713 0.34713 0.34713 670
输出
Date Open High Low Close Adj Close Volume
1 2007-06-02 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2 2007-06-07 0.34713 0.34713 0.34713 0.34713 0.34713 670
我尝试过的代码:
df_output = df.loc[df.Date.isin(df.Date + pd.offsets.BWeekEnd(1))]
答案 0 :(得分:1)
这对你有帮助吗?
import pandas as pd
import numpy as np
df = pd.DataFrame({"Date": ["2007-06-01",
"2007-06-02",
"2007-06-04",
"2007-06-05",
"2007-06-06",
"2007-06-07"],
"High": [0.33979,
0.33074,
0.33526,
0.32113,
0.34713,
0.34713]})
df["week"] = df["Date"].astype("M8[us]").dt.week
last_day = df.groupby("week")["Date"].max().values
del df["week"]
df[df["Date"].isin(last_day)]