I'm trying to figure out how to start a loop in Python that goes through a csv file. I believe it would be a while loop (can't use pandas for this assignment) but I'm not sure how to start. The file is from Kaggle - analyzing a page from Reddit trying to get the following:
the average number of comments across all posts the average score across all posts what the highest score is and the title for that post what the lowest score is and the title for that post what the most commented post is with its title and number of comments
this is what I have so far for importing the file:
import csv #import csv file reddit_vm.csv
def analyze(entries):
print(f'first entry: {entries[0]}')
with open("reddit_vm.csv", "r", encoding='UTF-8', errors="ignore") as input:
entries = [(e['id'], int(e['score']), int(e['comms_num']), e['title']) for e in csv.DictReader(input)]
avgScore = analyze(entries)
and this is what I think I need to do:
pseudocode:
need a variable to control the loop reading the lines while loop
average the number of comments across all posts
average score across all posts
largest variable for the highest score and print title smallest variable for lowest score
most_comments

pd.read_csv("yourcsvfile.csv"), you will get dataframe which will be much easier to handle and work with.