-1

I have a csv file of stock prices for each trading day for 9 years. how do i get the last trading day of each month and the respective prices?

I have tried grouping by months followed by the largest day but it doesn't seem to be working. Any guidance or suggestions pls

file:///var/folders/76/qqn_44f945564bdvv8dw_0jc0000gn/T/com.apple.Safari/WebKitDropDestination-wOBqM5Fs/Screenshot%202019-08-19%20at%205.03.22%20PM.png

import pandas as pd
data=pd.read_csv('csv_file')
data
type(data.index)
data.set_index('date',inplace=True)

Apologies, its my first time using this so i don't really know how to post the code. But this is the code i have so far. The url is the result of the csv data.

olleh
  • 23
  • 1
  • 5

2 Answers2

1

You can use

df.groupby([pd.Grouper(key = 'column_containing date', freq = 'M')])['column_containing date'].last()
moys
  • 7,747
  • 2
  • 11
  • 42
0

If your date data is part of the index you can use

df.groupby(df.index.strftime('%Y-%m')).tail(1)

psn1997
  • 144
  • 9