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 = df[df['Exit Date'].notnull()]['Age'].median()
  26. print(median_age)
  27. print(df[["Age", "Exit Date"]])
  28.  
Success #stdin #stdout 0.37s 59496KB
stdin
Standard input is empty
stdout
43.5
   Age  Exit Date
0   56 2021-12-31
1   42        NaT
2   29        NaT
3   46        NaT
4   30 2021-12-31
5   26        NaT
6   35        NaT
7   39        NaT
8   35 2021-12-31
9   52 2021-12-31