简体   繁体   中英

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.

My dataframe index consists of dates from 2019 to 2021 (datetime data type). I need to filter the DF so that only the data for the first day of each month shows. The first day for each month in the index is not always n-01 (its the first business day of the month). I am working with the Pandas library.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM