import pandas as pd
# Example test data for 5 stocks in the Consumer Discretionary sector
df = pd.DataFrame({
"Instrument": ["A", "B", "C", "D", "E"],
"TR.TotalNetIncome": [100, 200, 150, -50, 300], # Net income
"TR.AverageTotalEquity": [1000, 2000, 1200, 500, 2500] # Equity
})
# Calculate ROE (Return on Equity = Net Income / Equity)
df["ROE"] = df["TR.TotalNetIncome"] / df["TR.AverageTotalEquity"]
# Calculate average ROE for the entire sector
average_roe = df["ROE"].mean()
print("ROE per stock:")
print(df[["Instrument", "ROE"]])
print("\nAverage Return on Equity of the sector: {:.2%}".format(average_roe))
i want something like this, where i'm getting the Dataframe from the API and then I calculate all the results and make the average for the whole sector.