fork download
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. # Generate a random DataFrame
  5. data = {
  6. 'Employee ID': np.random.randint(1000, 9999, size=10),
  7. 'Full Name': ['Employee ' + str(i) for i in range(1, 11)],
  8. 'Job Title': np.random.choice(['Manager', 'Developer', 'Analyst', 'HR'], size=10),
  9. 'Department': np.random.choice(['IT', 'HR', 'Finance', 'Marketing'], size=10),
  10. 'Business Unit': np.random.choice(['Unit A', 'Unit B', 'Unit C'], size=10),
  11. 'Gender': np.random.choice(['Male', 'Female'], size=10),
  12. 'Ethnicity': np.random.choice(['Ethnicity 1', 'Ethnicity 2', 'Ethnicity 3'], size=10),
  13. 'Age': np.random.randint(20, 60, size=10),
  14. 'Hire Date': pd.date_range('2010-01-01', periods=10, freq='Y'),
  15. 'Annual Salary': np.random.randint(30000, 100000, size=10),
  16. 'Bonus %': np.random.uniform(5, 20, size=10),
  17. 'Country': np.random.choice(['USA', 'Canada', 'UK'], size=10),
  18. 'City': np.random.choice(['New York', 'Toronto', 'London'], size=10),
  19. 'Exit Date': pd.to_datetime(np.random.choice([None, '2021-12-31'], size=10))
  20. }
  21.  
  22. df = pd.DataFrame(data)
  23.  
  24. # Query to calculate the median age of employees who left the company
  25. median_age_of_exited_employees = df[df['Exit Date'].notna()]['Age'].median()
  26. print(median_age_of_exited_employees)
  27. print(df)
  28.  
Success #stdin #stdout 0.4s 59796KB
stdin
Standard input is empty
stdout
39.5
   Employee ID    Full Name  Job Title  ... Country      City  Exit Date
0         5256   Employee 1         HR  ...      UK  New York 2021-12-31
1         7103   Employee 2    Analyst  ...  Canada    London 2021-12-31
2         3717   Employee 3         HR  ...      UK    London        NaT
3         7895   Employee 4    Analyst  ...      UK    London        NaT
4         7917   Employee 5    Analyst  ...      UK  New York 2021-12-31
5         3672   Employee 6    Manager  ...  Canada  New York 2021-12-31
6         8696   Employee 7         HR  ...     USA  New York        NaT
7         5507   Employee 8  Developer  ...     USA  New York        NaT
8         5973   Employee 9  Developer  ...  Canada   Toronto        NaT
9         8965  Employee 10  Developer  ...      UK  New York        NaT

[10 rows x 14 columns]