Python set-up

library('reticulate')
use_condaenv(condaenv = 'STOCK_MASTER', required = TRUE)
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)


Libraries

import pandas as pd
import numpy as np


Data

df_raw = pd.read_csv('https://raw.githubusercontent.com/Brent-Morrison/Misc_scripts/master/stock_data.csv')
df_raw['date_stamp'] = pd.to_datetime(df_raw['date_stamp'], format="%d/%m/%Y")


Top n by group

Select columns and date required

df = df_raw[df_raw['date_stamp'] == '2021-06-30'][['symbol','date_stamp','sector','asset_growth','roa','roe','leverage','ttm_earnings']]


Order

df.sort_values(by=['sector','ttm_earnings'], ascending=[True,False], inplace=True)


Filter for top 5

df = df.groupby('sector').head(5).reset_index(drop=True).copy()


Print

rmarkdown::paged_table(py$df)