I get stuck in the relation between tags

I read the solution but I have no idea do the code mean, i copy the code to see the result, but it showed error.Could anyone help explain what we are doing at this step and why the code does not work? Thank you!

Screen Link: https://app.dataquest.io/m/469/guided-project%3A-popular-data-science-questions/9/enter-domain-knowledge

My Code:

relations_most_used = associations.loc[most_used.index, most_used.index]

def style_cells(x):
    helper_df = pd.DataFrame('', index=x.index, columns=x.columns)
    helper_df.loc["time-series", "r"] = "background-color: yellow"
    helper_df.loc["r", "time-series"] = "background-color: yellow"
    for k in range(helper_df.shape[0]):
        helper_df.iloc[k,k] = "color: blue"
    
    return helper_df

relations_most_used.style.apply(style_cells, axis=None)

The first line creates a new dataframe by filtering the associations dataframe by the most used and most viewed tags.

Then what the function does is that it takes in a dataframe and adds some color to it. The next two lines add a yellow background to values in the column r and row time-series or vice-versa:

helper_df.loc["time-series", "r"] = "background-color: yellow"
helper_df.loc["r", "time-series"] = "background-color: yellow"

And then you add the color blue to the values in the dataframe diagonal, which are values whose column index is the same as the row index:

for k in range(helper_df.shape[0]):
        helper_df.iloc[k,k] = "color: blue"

Finally, you use the style.apply to apply these changes.

To read more about styling dataframes, check this article.

Thank you for sharing the article. it answered all my questions .