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:
-
I would like to know how many male students have left the course.
-
Which city had more students who dropped out of the course.
-
By year, what was the number of students from public and private schools.
Where should I start and how should I code it?
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')
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:
-
I would like to know how many male students have left the course.
-
Which city had more students who dropped out of the course.
-
By year, what was the number of students from public and private schools.
Where should I start and how should I code it?
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')