Updating the Dataframe
Solution
df["Group"] = model.labels_
print(df)
We are going to want to look at different groups and see what they look like. First, I want to show you how to get a truth series that represents all the stocks in a group.
print(df["Group"]==1)
You index by it the same way as before.
print(df[df["Group"]==1])
Let’s loop through each group and see what stocks are in it.
for x in range(8):
print(df[df["Group"]==x])
Our next step is going to be bringing in the full table we downloaded from wikipedia and using it so that we can get information about each group’s industries.
SP500 = pd.DataFrame.from_csv("SP500.csv", encoding="UTF-8")
tickers = []
for x in SP500["Ticker"].values:
if "." in x:
x = x.replace(".","")
tickers.append(x)
SP500["Ticker"]=tickers
The extra code just ensures we have the correct version of each ticker so the two datasets will match.
Challenge
Set the index of the SP 500 datset to be the ticker, and then merge our two dataframes together.