Let\'s suppose that I have a dataframe with two columns in pandas which resembles the following one:
pandas
text label 0
top_terms = pd.DataFrame(columns = range(1,6)) for i in term_doc_mat.index: top_terms.loc[len(top_terms)] = term_doc_mat.loc[i].sort_values(ascending = False)[0:5].index
This will give you the top 5 terms for each document. Adjust as needed.