Last active
June 16, 2023 22:15
-
-
Save konverner/e4084377eec42d0752e2c20eb1d28b1d to your computer and use it in GitHub Desktop.
create world cloud from dataframe column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wordcloud import WordCloud | |
def create_wordcloud_from_column(df, column_name, title, max_token_len=3): | |
preprocessed_tokens = [] | |
for tokens in df[column_name].str.split().tolist(): | |
try: | |
for token in tokens: | |
if len(token) > max_token_len: | |
preprocessed_tokens.append(token.lower().strip().strip('-').strip('.')) | |
except: | |
pass | |
wordcloud = WordCloud(width=800, height=400, | |
background_color='white').generate(' '.join(preprocessed_tokens)) | |
plt.figure(figsize=(10, 5)) | |
plt.imshow(wordcloud, interpolation='bilinear') | |
plt.title(title) | |
plt.axis('off') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment