I want to create a data frame with Mondays of every month as index. Ex:
Date | blah | blah |
---|---|---|
4/7/2022. | . | . |
11/7/2022 | . | . |
18/7/2022 | ||
25/7/2022. |
How can I get that using python
CodePudding user response:
You can use pandas.date_range
, here setting the number of values to 20
(you could also chose an end date):
pd.date_range('2022-07-04', periods=20, freq='7D')
or, more automated:
pd.date_range('2022-07-01', periods=20, freq='W-MON')
as string:
pd.date_range('2022-07-01', periods=20, freq='W-MON').strftime('%d/%m/%Y')
example:
option1 option2 option3
0 2022-07-04 2022-07-04 04/07/2022
1 2022-07-11 2022-07-11 11/07/2022
2 2022-07-18 2022-07-18 18/07/2022
3 2022-07-25 2022-07-25 25/07/2022
...
16 2022-10-24 2022-10-24 24/10/2022
17 2022-10-31 2022-10-31 31/10/2022
18 2022-11-07 2022-11-07 07/11/2022
19 2022-11-14 2022-11-14 14/11/2022