简体   繁体   English

如何过滤我的数据框以仅显示每个月的第一个工作日?

[英]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.

相关问题 我怎样才能在一个月的第一天对 pandas DatetimeIndex 重新采样 - how can i resample a pandas DatetimeIndex by 1st of month 如何在 Pandas 中 select '本月的最后一个工作日'? - How can I select 'last business day of the month' in Pandas? 按月的工作日对 DataFrame 进行分组 - Group DataFrame by Business Day of Month 如果行的开始日期在今天起 90 天内,我如何过滤掉行并将它们放置到 R 中的下个月的 1 号? - How can I filter rows out if their start date is within 90 days from today and place them out until the 1st of the following month in R? 如何根据第一级最大值过滤MultiIndex数据帧? - How to filter MultiIndex dataframe based on 1st level max values? 如何按特定月/日过滤日期的 dataframe? - How to filter a dataframe of dates by a particular month/day? 如何通过第一列值简单地过滤 3d numpy 数组? - How can I simply filter a 3d numpy array by its 1st column values? 如何格式化 x 轴日期以在每个月的开始显示月份名称,然后仅显示天数? - how can I format x-axis date to show month-name on the start of every month, then day numbers only? fillna() 仅填充 dataframe 的第一个值 - fillna() only fills the 1st value of the dataframe 我怎么能像这样循环但得到一切而不是只有第一个 - how can i loop like this but getting everything instead of only the 1st one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM