Last active
October 15, 2022 23:25
-
-
Save rsalaza4/a920dc0deb6c81aa96a59499d279808f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Replace null values with string "no" (i.e., meaning that employee has not left the company) | |
| df['Departure Date'] = df['Departure Date'].fillna("no") | |
| # Get a list of all months from the hiring date of the first employee until current month | |
| all_months = pd.date_range(df["Hiring Date"].unique()[0], datetime.datetime.today().strftime('%m/%d/%Y'), freq='MS').strftime("%Y-%b").tolist() | |
| # Initialize list for cummulative employees | |
| cummulative_employees = [] | |
| # Loop through all the months in list | |
| for past_month in all_months: | |
| # Initialize counter of active employees for given month | |
| current_employees_in_month = 0 | |
| # Loop through all the employees | |
| for i in range(0,len(df)): | |
| # If employee was hired on or before current month and has not departed | |
| if pd.to_datetime(df["Hiring Date"][i]) <= pd.to_datetime(past_month) and (df["Departure Date"][i] == "no"): | |
| current_employees_in_month += 1 | |
| # If employee has departed and was hired on or before current month and their departure date is greater than current month | |
| if df["Departure Date"][i] != "no" and pd.to_datetime(df["Hiring Date"][i]) <= pd.to_datetime(past_month) and pd.to_datetime(df["Departure Date"][i]) > pd.to_datetime(past_month): | |
| current_employees_in_month += 1 | |
| # Append total cummulative employees for given month | |
| cummulative_employees.append(current_employees_in_month) | |
| # Create dataframe with active employees count per month | |
| employees_df = pd.DataFrame({"Date":all_months, "Count Active Employees":cummulative_employees}) | |
| # Parse Date column as datetime and set it as index | |
| employees_df["Date"] = pd.to_datetime(employees_df["Date"]) | |
| employees_df.set_index("Date", inplace=True) | |
| # Display bottom rows | |
| employees_df.tail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment