I have a dataframe where each row has an author(news channel), the title of the article, and number of comments on that article.
Basically, I want to calculate the number of comments on each article. I currently have the following code, but I want to factor it.
# CSV of news articles, with authors, articles, and comments
df = pd.read_csv('articles.csv')
# Counts per author
art_count = df['AUTHOR'].value_counts()
# Calculate # of comments per article
def comment_sum(df, channel, channel_name, target):
# ex) sum(df[df['AUTHOR'] == 'NYTIMES']['COMMENTS'])
return sum(df[df[channel] == channel_name][target])
# Calculate # of comments
com_count = []
for newspaper in art_count.index:
com_count.append(comment_sum(df,'AUTHOR',newspaper,'COMMENTS'))
I feel as if I can simplify my code, without declaring a method, by using a map and lambda function, but I'm unsure how to go about it.