Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wordcloud_columns script #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/wordcloud/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
wordcloud==1.9.3
pandas==2.*
51 changes: 51 additions & 0 deletions src/wordcloud/wordcloud_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Generate wordcloud from Pandas columns

By default generates for all columns

Use -e, --exclude with a comma separated list to
exclude columns.

Requirements: wordcloud pandas
Install: pip install -r requirements.txt

Usage:

python wordcloud_columns.py <file> [--exclude=COLUMNS]
"""

import argparse
from pathlib import Path

import wordcloud
import pandas as pd

# Additional stopwords
STOPWORDS = {"research", "University", "School", "Centre", "UK", "Institute"}

# Set global wordcloud settings here
# https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud.WordCloud
W = wordcloud.WordCloud(stopwords=set(wordcloud.STOPWORDS) | STOPWORDS)


def generate_column(df, column: str):
assert column in df.columns, f"Column {column} not found in dataframe"
return W.generate(" ".join(df[column]))


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate wordcloud for data columns")
parser.add_argument("file", help="Filename to parse")
parser.add_argument("-e", "--exclude", help="Exclude columns")
args = parser.parse_args()

df = pd.read_csv(args.file)
columns = df.columns
excluded_columns = []
if args.exclude:
excluded_columns = args.exclude.split(",")
columns = [c for c in columns if c not in excluded_columns]
for c in columns:
output_filename = Path(args.file).stem + "_" + c.replace(" ", "-").replace(",", "") + ".jpg"
generate_column(df, c).to_file(output_filename)
print(output_filename)