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)
  28.  
Success #stdin #stdout 0.38s 59756KB
stdin
Standard input is empty
stdout
47.5
   Employee ID    Full Name  Job Title  ... Country      City  Exit Date
0         7927   Employee 1  Developer  ...      UK  New York 2021-12-31
1         5509   Employee 2    Analyst  ...      UK    London 2021-12-31
2         2857   Employee 3         HR  ...      UK    London        NaT
3         5799   Employee 4    Analyst  ...      UK  New York        NaT
4         3690   Employee 5    Analyst  ...  Canada    London        NaT
5         9251   Employee 6         HR  ...     USA   Toronto        NaT
6         1331   Employee 7         HR  ...     USA  New York        NaT
7         2445   Employee 8    Manager  ...  Canada    London        NaT
8         1809   Employee 9    Analyst  ...  Canada   Toronto        NaT
9         1110  Employee 10    Analyst  ...     USA   Toronto        NaT

[10 rows x 14 columns]