[英]How can I filter my dataframe to only show the 1st business day of each month?
I am new to coding in Python, so a bit of help would be appreciated.我是 Python 编码的新手,因此请提供一些帮助。
My dataframe index consists of dates from 2019 to 2021 (datetime data type).我的数据帧索引由 2019 年到 2021 年的日期组成(日期时间数据类型)。 I need to filter the DF so that only the data for the first day of each month shows.
我需要过滤 DF,以便只显示每个月第一天的数据。 The first day for each month in the index is not always n-01 (its the first business day of the month).
索引中每个月的第一天并不总是 n-01(它是该月的第一个工作日)。 I am working with the Pandas library.
我正在使用 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.