situational count – python

Question:

I have a spreadsheet in excel, named as Students

Year - City - Anniversary - Gender - Course Status - School Origin

 2018 - SP - 13/05/1990 - M - Registered - Public
 2019 - RJ - 12/05/1990 - F - Registered - Particular
 2017 - SP - 13/05/1990 - M - Closed Enrollment - Public

I’m using google colab, I know how to count the values, but I don’t know how to do a situational count, for example:

  1. I would like to know how many male students have left the course.

  2. Which city had more students who dropped out of the course.

  3. By year, what was the number of students from public and private schools.

Where should I start and how should I code it?

Asked By: Skye

||

Answers:

df is your dataframe , just combining this answer and this answer

import pandas as pd
df = pd.read_excel('sheet.xlsx')
count_males_left = int(df[(df["Gender"]=="M") & (df["Course Status"]!="Registered")].count()[0])
df_cities_count = df.groupby(['City']).size().reset_index(name='count')
df_public_by_year = df[df["School Origin"] == "Public"].groupby(['Year']).size().reset_index(name='count')
Answered By: Ahmed AEK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.