Home > Blockchain >  How to find the last monday's data only from a dataframe in python?
How to find the last monday's data only from a dataframe in python?

Time:05-01

I have a dataframe that contains 1 years of weekly OHLC data. What do I need ?

  • list only the last monday's data of each month. For example, May has 5 weeks and I want to list the last monday's data of may and need to discard the rest. Here's the code that I tried and I'm able to list the data on weekly basis. I got stuck here! Any help would be appreciated!
import pandas as pd
import yfinance as yf
import datetime
from datetime import date, timedelta

periods=pd.date_range(start='2021-4-30',periods=60,freq='W')

start = periods[0].strftime('%Y-%m-%d')
end = periods[-1].strftime('%Y-%m-%d')
symbol="^NSEI"
df=yf.download(symbol,start,end,interval="1wk",index=periods)

CodePudding user response:

You can use groupby(pd.Grouper()) to group by month and get the latest record.

# reset index to flatten columns
df = df.reset_index()
# copy date column to label last monday of a month
df['last_monday_of_month'] = df['Date']
# groupby month and get latest record
df.groupby(pd.Grouper(freq='M', key='Date')).last().reset_index()
  • Related