from turftopic.namers import OpenAITopicNamer import os # OpenAI API key key to access GPT-4 os.environ["OPENAI_API_KEY"] = "" # use Namer to label topic model with LLM namer = OpenAITopicNamer("gpt-4o-mini") model.rename_topics(namer) # create a dataframe with labelled topics topics_df = model.topics_df() topics_df.columns = ['topic_id', 'topic_name', 'topic_words'] # split and explode topics_df['topic_word'] = topics_df['topic_words'].str.split(',') topics_df = topics_df.explode('topic_word') topics_df['topic_word'] = topics_df['topic_word'].str.strip() # add a rank for each word within a topic topics_df['word_rank'] = topics_df.groupby('topic_id').cumcount() + 1 # pivot to wide format wide = topics_df.pivot(index='word_rank', columns=['topic_id', 'topic_name'], values='topic_word')