1 from pyspark.sql import functions as f
2 from pyspark.sql import SparkSession
3 from pyspark.sql import Window
5 spark = SparkSession.builder.getOrCreate()
6 conf = spark.sparkContext.getConf()
8 df = spark.read.parquet("/gscratch/comdata/output/reddit_comments_by_subreddit.parquet")
11 df = df.filter(~df.subreddit.like("u_%"))
13 df = df.groupBy('subreddit').agg(f.count('id').alias("n_comments"))
15 win = Window.orderBy(f.col('n_comments').desc())
16 df = df.withColumn('comments_rank', f.rank().over(win))
20 df = df.sort_values("n_comments")
22 df.to_csv('/gscratch/comdata/output/reddit_similarity/subreddits_by_num_comments.csv', index=False)