![](/img/trans.png)
[英]how can i resample a pandas DatetimeIndex by 1st of month
[英]How can I filter my dataframe to only show the 1st business day of each month?
我是 Python 編碼的新手,因此請提供一些幫助。
我的數據幀索引由 2019 年到 2021 年的日期組成(日期時間數據類型)。 我需要過濾 DF,以便只顯示每個月第一天的數據。 索引中每個月的第一天並不總是 n-01(它是該月的第一個工作日)。 我正在使用 Pandas 庫。
df = pd.DataFrame(
{'Date': list(pd.date_range('2019-01-01', periods=50, freq='5D')) * 2, 'Value1': np.random.randint(0, 1000, 100),
'Value2': np.random.randint(0, 1000, 100)})
df = df.groupby([df.Date.dt.year, df.Date.dt.month], as_index=False).apply(
lambda x: x[x.Date.eq(x.Date.min())]).reset_index(drop=True)
print(df.head(10))
Date Value1 Value2
0 2019-01-01 183 942
1 2019-01-01 735 171
2 2019-02-05 335 456
3 2019-02-05 343 643
4 2019-03-02 865 423
5 2019-03-02 776 780
6 2019-04-01 402 16
7 2019-04-01 680 927
8 2019-05-01 718 994
9 2019-05-01 227 710
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.