SQL to Pandas Aggregate Syntax

Hi there!

I’m currently working on an external project and I can’t quite figure the correct syntax for this problem. I’m attempting to calculate an aggregate using Pandas and Numpy that would resemble the SQL query below:

SELECT *, SUM(Score) as ‘Top 5 Categories’ FROM df
WHERE (Content_type IS NOT NULL) AND (Score IS NOT NULL)
GROUP BY Category
ORDER BY 2 desc
LIMIT 5

The tables view:

My (rather disappointing ) attempt:
df['Top 5 Categories'] = (df.groupby("Category").agg({"Score": "sum"}) & (df["Content_type"].notna())).sort_values(by="Score",ascending=False).head()

Any suggestions on either breaking down the steps or a full solution will be appreciated :slight_smile: